thor
thor

Reputation: 22480

how to add qlabel to a QDialogButtonBox UI file

I'm trying to write a QGIS plugin and display a help dialogbox with python 3 and Qt5. However, the default dialog box based on QDialogButtonBox shows only two standard buttons and no text.

enter image description here

The .ui file has the following:

  <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>

I'm new to Qt5/pyqt. I tried to add a widget following examples:

label = QLabel()
label.setText("Help\n instructions: \n")
self.dlg.addWidget(label)

But I got an error: AttributeError: 'XXXDialog' object has no attribute 'addWidget'

My question is:

How can I add a QLabel or another widget (preferably with a text editor) that can display multiline text to the main area of the dialogbox. Do I have to use a different base class than QDialogButtonBox?

Upvotes: 0

Views: 3679

Answers (2)

Slavenskij
Slavenskij

Reputation: 649

QDialog does not have "addWidget" but its layout does.

  layout = QVBoxLayout()
  label = QLabel(self.dlg)
  label.setText("Help\n instructions: \n")
  layout.addWidget(label);
  self.dlg.setLayout(layout);

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 243955

A possible solution is to pass as QLabel parent to QDialog, since in Qt the coordinates of a widget are relative to the parent, then you can change the position with move(), to adjust the size of the QLabel to its content you must use adjustSize():

label = QLabel(self.dlg)
label.setText("Help\n instructions: \n")
label.adjustSize()
label.move(100, 60)

Upvotes: 1

Related Questions