在pyqt5 designer中QFrame和QWidget 分别啥作用
时间: 2024-05-29 16:12:39 浏览: 206
QFrame是一个带有边框和背景色的矩形框架,它可以作为容器来放置其他控件。QFrame可以设置不同的边框样式、背景色以及边框宽度等属性,从而实现不同的视觉效果。
QWidget是Qt中所有用户界面控件的基类,它提供了控件的基本功能,如大小调整、位置设置、绘制、事件处理等。QWidget可以用来创建不同种类的控件,如按钮、标签、文本框等。QWidget还可以作为容器,用来放置其他控件。
相关问题
在pyqt5 designer中QFrame作用
QFrame是一个用于创建矩形框架的QWidget子类。它允许您向窗口添加分隔线、边框和其他装饰性元素。
在PyQt5 Designer中,您可以使用QFrame来创建一个带有边框、背景颜色和/或图像的矩形框架。您可以将其他控件放置在QFrame内部,以便将它们组合在一起并使它们彼此分离。
例如,您可以在QFrame中添加两个QLabel控件和一个QPushButton控件,以创建一个自定义窗口部件,该部件包含一个标题和一个按钮。您可以使用QFrame来定义窗口部件的大小和样式,并使用其他控件来添加内容和交互元素。
pyqt5QtDesigner中的展开收缩栏折叠部分界面
### 实现 PyQt5 Qt Designer 中界面部分区域的展开与收缩
为了实现在 PyQt5 的 Qt Designer 中创建可展开和收缩的部分,通常会采用自定义的小部件来处理可见性和布局调整。一种常见的方式是利用 `QStackedWidget` 或者通过编程方式控制特定组件的高度变化以及其子组件的显示状态。
对于简单的展开/收缩功能来说,在设计阶段可以先准备好两个版本的状态——完全展示所有内容时的样子和平铺状态下仅保留标题或其他提示信息的形式;之后再编写逻辑代码去动态改变这些预设好的UI片段之间的切换[^1]。
具体到实现细节上:
- **准备基础 UI 结构**
在 Qt Designer 中构建好静态页面结构,包括那些可能会被隐藏或显现出来的小组件群组。比如设置一个按钮作为触发器用于开启关闭操作,并把要变动的内容放在垂直布局容器内以便于后续尺寸调节[^4]。
```python
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QFrame
class CollapsibleBox(QWidget):
def __init__(self, title="", parent=None):
super(CollapsibleBox, self).__init__(parent)
toggle_button = QPushButton(
f"{title} ▾", checked=False, checkable=True, clicked=self.on_pressed
)
toggle_animation = QtCore.QParallelAnimationGroup(self)
content_area = QScrollArea(maximumHeight=0, minimumHeight=0)
layout = QVBoxLayout(self)
main_layout = QVBoxLayout()
toggle_button.setStyleSheet("text-align:left;")
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(toggle_button)
layout.addWidget(content_area)
self.toggle_button = toggle_button
self.content_area = content_area
self.toggle_animation = toggle_animation
self.main_layout = main_layout
content_wrap = QWidget()
content_wrap.setLayout(main_layout)
content_area.setWidget(content_wrap)
content_area.setWidgetResizable(True)
@QtCore.pyqtSlot()
def on_pressed(self):
checked = self.toggle_button.isChecked()
self.toggle_button.setText(f"{'▾' if not checked else '▸'} {self.title}")
direction = (
QtCore.QAbstractAnimation.Forward
if checked
else QtCore.QAbstractAnimation.Backward
)
self.toggle_animation.setDirection(direction)
collapsed_height = (self.sizeHint().height() -
self.content_area.maximumHeight())
content_height = self.content_area.layout().sizeHint().height()
for i in range(self.toggle_animation.animationCount()):
animation = self.toggle_animation.animationAt(i)
animation.setDuration(300)
animation.setStartValue(collapsed_height)
animation.setEndValue(collapsed_height + content_height * bool(not checked))
content_animation = QtCore.QPropertyAnimation(
self,
b"minimumHeight",
duration=300,
startValue=collapsed_height,
endValue=collapsed_height +
(content_height *
int(not checked)),
)
self.toggle_animation.addAnimation(content_animation)
self.toggle_animation.start()
if __name__ == "__main__":
app = QApplication([])
window = QWidget()
v_box = QVBoxLayout(window)
collapsible_widget = CollapsibleBox(title="Collapsible Box Title")
label_example = QLabel('This is an example of a collapsible section.')
collapsible_widget.main_layout.addWidget(label_example)
v_box.addWidget(collapsible_widget)
window.show()
sys.exit(app.exec_())
```
这段脚本展示了如何创建一个带有标题栏并能响应点击事件来进行自身高度变换从而达到视觉上的开合效果的小部件实例。这里运用到了动画机制使得过渡更加平滑自然[^2]。
阅读全文
相关推荐
















