pyqt6/pyside6:如何按内容设置qframe大小?
我创建了一个qframe
,其中我将2 qpushbuttons
。
我将此qframe
与主窗口插入了主 qvboxlayout (主窗口小部件也是qframe
)。
主小部件
的高度要大得多,因为这是我的qframe
,其中两个按钮设置了主窗口的高度。
是否可以设置qframe
的大小,以便等于此Qframe
的内容的大小?
代码的一部分(qframe
带有按钮):
# buttons
self.buttons_layout = QVBoxLayout()
self.buttons_layout.setAlignment(Qt.AlignTop)
self.buttons_layout.setContentsMargins(0, 0, 0, 0)
self.btn1 = QPushButton("1")
self.btn2 = QPushButton("2")
self.buttons_layout.addWidget(self.btn1)
self.buttons_layout.addWidget(self.btn2)
self.btn1.clicked.connect(lambda: self.pages_layout.setCurrentIndex(0))
self.btn2.clicked.connect(lambda: self.pages_layout.setCurrentIndex(1))
self.buttons_widget = QFrame()
self.buttons_widget.setObjectName("buttons_frame")
self.buttons_widget.setStyleSheet(f"#buttons_frame{{margin-top: {self.bar.height()+6}px;}}")
self.buttons_widget.setLayout(self.buttons_layout)
结果:
我需要的内容:
I have created a QFrame
where I put 2 QPushButtons
.
I inserted this QFrame
into the main QVBoxLayout
along with the main widget (the main widget is also a QFrame
).
The main widget
is much larger in height, because of this my QFrame
with two buttons sets the height of the main widget.
Is it possible to set the size of a QFrame
so that it is equal to the size of the contents of this QFrame
?
Part of code (QFrame
with buttons):
# buttons
self.buttons_layout = QVBoxLayout()
self.buttons_layout.setAlignment(Qt.AlignTop)
self.buttons_layout.setContentsMargins(0, 0, 0, 0)
self.btn1 = QPushButton("1")
self.btn2 = QPushButton("2")
self.buttons_layout.addWidget(self.btn1)
self.buttons_layout.addWidget(self.btn2)
self.btn1.clicked.connect(lambda: self.pages_layout.setCurrentIndex(0))
self.btn2.clicked.connect(lambda: self.pages_layout.setCurrentIndex(1))
self.buttons_widget = QFrame()
self.buttons_widget.setObjectName("buttons_frame")
self.buttons_widget.setStyleSheet(f"#buttons_frame{{margin-top: {self.bar.height()+6}px;}}")
self.buttons_widget.setLayout(self.buttons_layout)
Result:
What I need:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论