0
我试图在垂直框架内包含一个包含标签的水平框架,但即使标签被显示出来也不在正确的位置,它仅限于标准QLabel子版本的QFrame没有调整到它的布局
这是主类:
class Launcher(QMainWindow):
def __init__(self):
super().__init__()
self.setFrame() #sets up window's geometry, works fine
self.setContent()
self.show()
def setContent(self):
layout = QBoxLayout(QBoxLayout.TopToBottom)
layout.addWidget(widgets.Logo(self), 0, Qt.AlignTop)
self.setLayout(layout)
这是从一个 “小部件” 进口类模块
class Logo(QFrame):
def __init__(self, parent):
super().__init__(parent)
layout = QBoxLayout(QBoxLayout.LeftToRight)
text = QLabel("PyTitle", self)
text.setAlignment(Qt.AlignCenter)
text.setFont(QFont("impact", 48))
layout.addWidget(text, 0, Qt.AlignCenter)
self.setLayout(layout)
self.show()
如果我强制调整QLabel和QFrame的大小,它是可见的,但仍然在左上角。
你有使用QBoxLayout而不是QHBoxLayout的原因吗?如果您只想从左到右堆叠一堆小部件,请使用QHBoxLayout,并且如果您希望它们从上到下堆叠使用QVBoxLayout。我永远不会与QBoxLayout混淆。从QBoxLayout的文档:“你几乎总是希望使用QVBoxLayout和QHBoxLayout而不是QBoxLayout,因为它们的构造函数很方便。” – neuronet