如何将 qlabel 添加到 QDialogButtonBox UI 文件
how to add qlabel to a QDialogButtonBox UI file
我正在尝试使用 python 3 和 Qt5 编写 QGIS 插件并显示帮助对话框。但是,基于 QDialogButtonBox
的默认对话框仅显示两个标准按钮且没有文本。
.ui
文件具有以下内容:
<widget class="QDialogButtonBox" name="button_box" >
<property name="geometry" >
<rect>
<x>30</x>
<y>240</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</widget>
我是 Qt5/pyqt 的新手。我尝试添加以下示例的小部件:
label = QLabel()
label.setText("Help\n instructions: \n")
self.dlg.addWidget(label)
但是我得到一个错误:AttributeError: 'XXXDialog' object has no attribute 'addWidget'
我的问题是:
如何添加 QLabel
或其他可以在对话框的主要区域显示多行文本的小部件(最好使用文本编辑器)。我必须使用与 QDialogButtonBox
不同的基数 class 吗?
一个可能的解决方案是将 QLabel
parent 传递给 QDialog
,因为在 Qt 中 widget 的坐标是相对于 parent 的,那么你可以用 [=13= 改变位置], 要将 QLabel
的大小调整为其内容,您必须使用 adjustSize()
:
label = QLabel(self.dlg)
label.setText("Help\n instructions: \n")
label.adjustSize()
label.move(100, 60)
QDialog 没有 "addWidget" 但它的布局有。
layout = QVBoxLayout()
label = QLabel(self.dlg)
label.setText("Help\n instructions: \n")
layout.addWidget(label);
self.dlg.setLayout(layout);
我正在尝试使用 python 3 和 Qt5 编写 QGIS 插件并显示帮助对话框。但是,基于 QDialogButtonBox
的默认对话框仅显示两个标准按钮且没有文本。
.ui
文件具有以下内容:
<widget class="QDialogButtonBox" name="button_box" >
<property name="geometry" >
<rect>
<x>30</x>
<y>240</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</widget>
我是 Qt5/pyqt 的新手。我尝试添加以下示例的小部件:
label = QLabel()
label.setText("Help\n instructions: \n")
self.dlg.addWidget(label)
但是我得到一个错误:AttributeError: 'XXXDialog' object has no attribute 'addWidget'
我的问题是:
如何添加 QLabel
或其他可以在对话框的主要区域显示多行文本的小部件(最好使用文本编辑器)。我必须使用与 QDialogButtonBox
不同的基数 class 吗?
一个可能的解决方案是将 QLabel
parent 传递给 QDialog
,因为在 Qt 中 widget 的坐标是相对于 parent 的,那么你可以用 [=13= 改变位置], 要将 QLabel
的大小调整为其内容,您必须使用 adjustSize()
:
label = QLabel(self.dlg)
label.setText("Help\n instructions: \n")
label.adjustSize()
label.move(100, 60)
QDialog 没有 "addWidget" 但它的布局有。
layout = QVBoxLayout()
label = QLabel(self.dlg)
label.setText("Help\n instructions: \n")
layout.addWidget(label);
self.dlg.setLayout(layout);