我本质上只是想在布局的顶部添加一个标题来给出一个简短的描述,但 Qt 已经妨碍了。
我启用了自动换行,并且QLabel
会根据内容的长度动态增加它的高度......但它总是太短。
我的代码:
class Window(QMainWindow):
def __init__(self, qtpy):
super().__init__()
self.qtpy = qtpy
self.setMinimumSize(250, 150)
self.resize(600, 450)
class ThemeTool(Window):
def __init__(self, qtpy):
super().__init__(qtpy)
self.setWindowTitle("qtpy-rant Theme Tool")
widget = QWidget()
layout = QBoxLayout(QBoxLayout.LeftToRight)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
widget.setLayout(layout)
self.setCentralWidget(widget)
sidebar = QWidget()
sidebar.setMaximumWidth(150)
sidebar_layout = QBoxLayout(QBoxLayout.TopToBottom)
sidebar_layout.setAlignment(Qt.AlignTop)
sidebar.setLayout(sidebar_layout)
layout.addWidget(sidebar)
palette = sidebar.palette()
sidebar.setAutoFillBackground(True)
palette.setColor(sidebar.backgroundRole(), QColor("#e3e3e3"))
sidebar.setPalette(palette)
sidebar_label = QLabel()
sidebar_label.setWordWrap(True)
sidebar_label.setText("The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors")
sidebar_label.adjustSize()
sidebar_layout.addWidget(sidebar_label)
palette = sidebar_label.palette()
sidebar_label.setAutoFillBackground(True)
palette.setColor(sidebar_label.backgroundRole(), QColor("#ff0000"))
sidebar_label.setPalette(palette)
theme_area = QWidget()
theme_area_layout = QBoxLayout(QBoxLayout.TopToBottom)
theme_area.setLayout(theme_area_layout)
layout.addWidget(theme_area, 1)
qapplication = QApplication(sys.argv)
main_window = ThemeTool(None)
main_window.show()
qapplication.exec_()