Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLineEdit doesn't fully expand on Mac



  • I'm finding that my QLineEdit widgets are not fully expanding to the space available, even when the horizontal size policy is set to Expanding. This makes my application look strange. It occurs both when I run a code straight from Python or when I use the Tools -> Form Editor -> Preview in -> macintosh Style in Qt Creator.

    Any workarounds? Is this a bug I should report?

    I'm using Qt 5.15.2 (I don't think other details are pertinent since the error occurs in Qt Creator preview, but I'm running macOS 10.15.7). Minimal working example follows.

    import sys
    from PySide2.QtWidgets import *
    from PySide2.QtCore import Qt
    
    def window():
       app = QApplication(sys.argv)
       win = QWidget()
    
       l1 = QLabel("Name")
       nm = QLineEdit()
    
       l2 = QLabel("Info")
       add1 = QTextEdit()
       fbox = QFormLayout()
       fbox.addRow(l1,nm)
       vbox = QVBoxLayout()
    
       vbox.addWidget(add1)
       fbox.addRow(l2,vbox)
       hbox = QHBoxLayout()
    
       r1 = QRadioButton("Male")
       r2 = QRadioButton("Female")
       hbox.addWidget(r1)
       hbox.addWidget(r2)
       hbox.addStretch()
       fbox.addRow(QLabel("sex"),hbox)
       fbox.addRow(QPushButton("Submit"),QPushButton("Cancel"))
    
       win.setLayout(fbox)
    
       win.setWindowTitle("PyQt")
       win.show()
       sys.exit(app.exec_())
    
    if __name__ == '__main__':
       window()
    

  • Lifetime Qt Champion

    Hi,

    You can modify the field growth policy to match the effect you want. Note that you will go against your platform GUI guidelines.



  • Here's a minimal working example UI file for opening in Qt Creator.

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>test</class>
     <widget class="QMainWindow" name="test">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>test</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <layout class="QFormLayout" name="formLayout_23">
          <item row="0" column="1">
           <widget class="QLabel" name="label_77"/>
          </item>
          <item row="1" column="0">
           <widget class="QLabel" name="label_83">
            <property name="text">
             <string>Title</string>
            </property>
           </widget>
          </item>
          <item row="2" column="0">
           <widget class="QLabel" name="label_79">
            <property name="text">
             <string>Notes</string>
            </property>
           </widget>
          </item>
          <item row="1" column="1">
           <widget class="QLineEdit" name="lineEdit"/>
          </item>
          <item row="2" column="1">
           <widget class="QPlainTextEdit" name="plainTextEdit"/>
          </item>
          <item row="0" column="0">
           <widget class="QLabel" name="label_74">
            <property name="text">
             <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;Include&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>22</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    


  • Hi,
    You are using QFormLayout, you should use QVBoxLayout instead.



  • @mpergand
    I'm rather confused by the suggestion. I'm trying to create a form with 2 columns (one column of labels, and one column of input widgets). How would I do this with a QVBoxLayout? If I first made each of the rows its own layout with a QHBoxLayout and then put all the rows into a QVBoxLayout, the labels won't line up...


  • Lifetime Qt Champion

    Hi,

    You can modify the field growth policy to match the effect you want. Note that you will go against your platform GUI guidelines.


Log in to reply