Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-89741

QLabel and QWidget(QComboBox and QSpinBox) do not align

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reported
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 5.15.2
    • Fix Version/s: None
    • Component/s: GUI: Look'n'Feel
    • Labels:
      None
    • Platform/s:
      macOS

      Description

      This bug is detailed in https://forum.qt.io/topic/122188/can-t-get-qlabel-qcombobox-and-qspinbox-to-align-in-form-layout.

      I'd like to group a QComboBox and QSpinBox in a Widget Container, and make that the "Field" corresponding to a QLabel in a Form Layout. However, the 3 widgets will not line up. If I set the Widget Container to a Form Layout, the ComboBox is above the SpinBox (top image in attachment). If I set the Widget Container to a Horizontal (middle image) or Grid Layout (bottom image), the ComboBox and Spinbox are aligned, but they're below the Label (even setting the layoutTopMargin of the Widget Container to 0).

      Per https://bugreports.qt.io/browse/QTBUG-2699, this should be solved by setting the Qt::WA_LayoutUsesWidgetRect attribute on the ComboBox. However, doing this in PySide2 is not fixing this issue.

      The minimal working example which generated the attached image is below.

       

      main.py:

      import sys
      import os
      
      from PySide2.QtWidgets import QApplication, QMainWindow
      from PySide2.QtCore import Qt
      
      from Ui_MainWindow import Ui_MainWindow
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super(MainWindow, self).__init__()
              self.ui = Ui_MainWindow()
              self.ui.setupUi(self)
      
              self.ui.comboBox_1.setAttribute(Qt.WA_LayoutUsesWidgetRect)
              self.ui.comboBox_2.setAttribute(Qt.WA_LayoutUsesWidgetRect)
              self.ui.comboBox_3.setAttribute(Qt.WA_LayoutUsesWidgetRect)
      
      if __name__ == "__main__":
          app = QApplication([])
          widget = MainWindow()
          widget.show()
          sys.exit(app.exec_())
      

       

      Ui_MainWindow.py:
      (I didn't touch this file; I just generated it via pyside2-uic form.ui -o Ui_MainWindow.py):

      from PySide2.QtCore import *
      from PySide2.QtGui import *
      from PySide2.QtWidgets import *
      
      
      class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
              if not MainWindow.objectName():
                  MainWindow.setObjectName(u"MainWindow")
              MainWindow.resize(800, 670)
              self.centralwidget = QWidget(MainWindow)
              self.centralwidget.setObjectName(u"centralwidget")
              self.layoutWidget = QWidget(self.centralwidget)
              self.layoutWidget.setObjectName(u"layoutWidget")
              self.layoutWidget.setGeometry(QRect(240, 100, 279, 52))
              self.formLayout_1 = QFormLayout(self.layoutWidget)
              self.formLayout_1.setObjectName(u"formLayout_1")
              self.formLayout_1.setContentsMargins(0, 0, 0, 0)
              self.radioButton_1 = QRadioButton(self.layoutWidget)
              self.radioButton_1.setObjectName(u"radioButton_1")
      
              self.formLayout_1.setWidget(0, QFormLayout.LabelRole, self.radioButton_1)
      
              self.widget_1 = QWidget(self.layoutWidget)
              self.widget_1.setObjectName(u"widget_1")
              self.formLayout = QFormLayout(self.widget_1)
              self.formLayout.setObjectName(u"formLayout")
              self.formLayout.setContentsMargins(-1, 0, -1, -1)
              self.doubleSpinBox_1 = QDoubleSpinBox(self.widget_1)
              self.doubleSpinBox_1.setObjectName(u"doubleSpinBox_1")
      
              self.formLayout.setWidget(0, QFormLayout.LabelRole, self.doubleSpinBox_1)
      
              self.comboBox_1 = QComboBox(self.widget_1)
              self.comboBox_1.setObjectName(u"comboBox_1")
      
              self.formLayout.setWidget(0, QFormLayout.FieldRole, self.comboBox_1)
      
      
              self.formLayout_1.setWidget(0, QFormLayout.FieldRole, self.widget_1)
      
              self.layoutWidget1 = QWidget(self.centralwidget)
              self.layoutWidget1.setObjectName(u"layoutWidget1")
              self.layoutWidget1.setGeometry(QRect(260, 240, 281, 52))
              self.formLayout_2 = QFormLayout(self.layoutWidget1)
              self.formLayout_2.setObjectName(u"formLayout_2")
              self.formLayout_2.setContentsMargins(0, 0, 0, 0)
              self.radioButton_2 = QRadioButton(self.layoutWidget1)
              self.radioButton_2.setObjectName(u"radioButton_2")
      
              self.formLayout_2.setWidget(0, QFormLayout.LabelRole, self.radioButton_2)
      
              self.widget_2 = QWidget(self.layoutWidget1)
              self.widget_2.setObjectName(u"widget_2")
              self.horizontalLayout = QHBoxLayout(self.widget_2)
              self.horizontalLayout.setObjectName(u"horizontalLayout")
              self.horizontalLayout.setContentsMargins(-1, 0, -1, -1)
              self.doubleSpinBox_2 = QDoubleSpinBox(self.widget_2)
              self.doubleSpinBox_2.setObjectName(u"doubleSpinBox_2")
      
              self.horizontalLayout.addWidget(self.doubleSpinBox_2)
      
              self.comboBox_2 = QComboBox(self.widget_2)
              self.comboBox_2.setObjectName(u"comboBox_2")
      
              self.horizontalLayout.addWidget(self.comboBox_2)
      
      
              self.formLayout_2.setWidget(0, QFormLayout.FieldRole, self.widget_2)
      
              self.widget = QWidget(self.centralwidget)
              self.widget.setObjectName(u"widget")
              self.widget.setGeometry(QRect(250, 340, 281, 52))
              self.formLayout_3 = QFormLayout(self.widget)
              self.formLayout_3.setObjectName(u"formLayout_3")
              self.formLayout_3.setContentsMargins(0, 0, 0, 0)
              self.radioButton_3 = QRadioButton(self.widget)
              self.radioButton_3.setObjectName(u"radioButton_3")
      
              self.formLayout_3.setWidget(0, QFormLayout.LabelRole, self.radioButton_3)
      
              self.widget_3 = QWidget(self.widget)
              self.widget_3.setObjectName(u"widget_3")
              self.gridLayout = QGridLayout(self.widget_3)
              self.gridLayout.setObjectName(u"gridLayout")
              self.gridLayout.setContentsMargins(-1, 0, -1, -1)
              self.doubleSpinBox_3 = QDoubleSpinBox(self.widget_3)
              self.doubleSpinBox_3.setObjectName(u"doubleSpinBox_3")
      
              self.gridLayout.addWidget(self.doubleSpinBox_3, 0, 0, 1, 1)
      
              self.comboBox_3 = QComboBox(self.widget_3)
              self.comboBox_3.setObjectName(u"comboBox_3")
      
              self.gridLayout.addWidget(self.comboBox_3, 0, 1, 1, 1)
      
      
              self.formLayout_3.setWidget(0, QFormLayout.FieldRole, self.widget_3)
      
              MainWindow.setCentralWidget(self.centralwidget)
              self.menubar = QMenuBar(MainWindow)
              self.menubar.setObjectName(u"menubar")
              self.menubar.setGeometry(QRect(0, 0, 800, 22))
              MainWindow.setMenuBar(self.menubar)
              self.statusbar = QStatusBar(MainWindow)
              self.statusbar.setObjectName(u"statusbar")
              MainWindow.setStatusBar(self.statusbar)
      
              self.retranslateUi(MainWindow)
      
              QMetaObject.connectSlotsByName(MainWindow)
          # setupUi
      
          def retranslateUi(self, MainWindow):
              MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
              self.radioButton_1.setText(QCoreApplication.translate("MainWindow", u"RadioButton", None))
              self.radioButton_2.setText(QCoreApplication.translate("MainWindow", u"RadioButton", None))
              self.radioButton_3.setText(QCoreApplication.translate("MainWindow", u"RadioButton", None))
          # retranslateUi
      
      

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            qt.team.quick.subscriptions Qt Quick and Widgets Team
            Reporter:
            efremdan1 Efrem Braun
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes