Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Problem with QtFormLayout
Forum Updated to NodeBB v4.3 + New Features

Problem with QtFormLayout

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 3 Posters 671 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • FurquimF Offline
    FurquimF Offline
    Furquim
    wrote on last edited by
    #1

    Hello, guys!

    I'm new to PyQt5 and I have a basic question that I couldn't solve. I use pyqt designer to organize the layout and widgets.

    Im trying to make a form like this (a space between the Label and the LineEdit):
    1.PNG
    But evey time I remove the first line it stays like this:
    2.PNG
    I don't why this is happening. The first row ("TextLabel") I made using the designer and only changed the MaximumWidth and MinimumWidth for the label text and the LineEdit.

    I did the same thing in the code. I only modified the MaximumWidth and MinimumWidth for each component in the row.

    Here is my code:

        def main():
            variable_names=["test1","test2"]
            self.create_form(variable_names)
            self.clear_layout(self.formLayout_output)
            self.create_form(variable_names)
    
        def create_form(self, variable_names: list):
            self.lineEdit_out_objs = [QLineEdit() for i in range( (len(variable_names) ) )]
            self.label_out_objs = [QLineEdit() for i in range( (len(variable_names) ) )]
            print(self.label_outTemplate.minimumWidth())
    
            for i in range(len(variable_names)):
                self.formLayout_output.addRow(variable_names[i], self.lineEdit_out_objs[i])
    
                #config Labels proprieties here:
                self.label_out_objs[i].setMinimumWidth(self.label_outTemplate.minimumWidth())
                self.label_out_objs[i].setMaximumWidth(self.label_outTemplate.maximumWidth())
    
                #config LineEdit proprieties here:
                self.lineEdit_out_objs[i].setMinimumWidth(self.lineEdit_outTemplate.minimumWidth())
                self.lineEdit_out_objs[i].setMaximumWidth(self.lineEdit_outTemplate.maximumWidth())
        
        def clear_layout(self, layout):
                for i in reversed(range(layout.count())): 
                    widgetToRemove = layout.itemAt(i).widget()
                    # remove it from the layout list
                    layout.removeWidget(widgetToRemove)
                    # remove it from the gui
                    widgetToRemove.setParent(None)
    

    And here is the layout configuration in Qt Desginer:
    3.PNG
    Ps: Im using Python 3.8.2 and PyQt5 5.13.2

    Thanks for your attention!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should be able to get what you want using both formAlignment and labelAlignment.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • FurquimF Offline
        FurquimF Offline
        Furquim
        wrote on last edited by
        #3

        Thanks SGaist for you answer but I already tryed adding these two lines of code:

        self.formLayout_output.setFormAlignment(QtCore.Qt.AlignRight)
        self.formLayout_output.setLabelAlignment(QtCore.Qt.AlignLeft)
        

        And it took no effect. So now it looks like this:

            def main():
                variable_names=["test1","test2"]
                self.create_form(variable_names)
                self.clear_layout(self.formLayout_output)
                self.create_form(variable_names)
        
            def create_form(self, variable_names: list):
                self.lineEdit_out_objs = [QLineEdit() for i in range( (len(variable_names) ) )]
                self.label_out_objs = [QLineEdit() for i in range( (len(variable_names) ) )]
                print(self.label_outTemplate.minimumWidth())
        
                for i in range(len(variable_names)):
                    self.formLayout_output.addRow(variable_names[i], self.lineEdit_out_objs[i])
        
                    #config Labels proprieties here:
                    self.label_out_objs[i].setMinimumWidth(self.label_outTemplate.minimumWidth())
                    self.label_out_objs[i].setMaximumWidth(self.label_outTemplate.maximumWidth())
        
                    #config LineEdit proprieties here:
                    self.lineEdit_out_objs[i].setMinimumWidth(self.lineEdit_outTemplate.minimumWidth())
                    self.lineEdit_out_objs[i].setMaximumWidth(self.lineEdit_outTemplate.maximumWidth())
        
            self.formLayout_output.setFormAlignment(QtCore.Qt.AlignRight)
            self.formLayout_output.setLabelAlignment(QtCore.Qt.AlignLeft)
            
            def clear_layout(self, layout):
                    for i in reversed(range(layout.count())): 
                        widgetToRemove = layout.itemAt(i).widget()
                        # remove it from the layout list
                        layout.removeWidget(widgetToRemove)
                        # remove it from the gui
                        widgetToRemove.setParent(None)
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then please provide a complete minimal example to allow testing.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          FurquimF 1 Reply Last reply
          0
          • SGaistS SGaist

            Then please provide a complete minimal example to allow testing.

            FurquimF Offline
            FurquimF Offline
            Furquim
            wrote on last edited by
            #5

            @SGaist

            Here is a test code:

            import sys, os
            from PyQt5 import QtCore, QtGui, QtWidgets, uic
            from PyQt5.QtGui import QIcon, QDoubleValidator
            from PyQt5.QtWidgets import QPushButton, QFileDialog, QLabel, QMessageBox, QTextBrowser, QButtonGroup, QFormLayout, QLineEdit, QSizePolicy, QSpacerItem
            
            class MyWindow(QtWidgets.QMainWindow):
                def __init__(self):
                    super(MyWindow,self).__init__()
                    self.setFixedSize(800, 600)
                    self.window_title="GALIA"
                    self.unitCell_ui()
            
            #SCREEN
            
                def unitCell_ui (self):
                    uic.loadUi('interface.ui',self)
                    self.setWindowTitle(self.window_title)
            
                    variable_names=["test1","test2"]
                    
                    self.create_form(variable_names)
                    self.clear_layout(self.formLayout_output)
                    self.create_form(variable_names)
            
            #FUNCTIONS
            
                def clear_layout(self, layout):
                        for i in reversed(range(layout.count())): 
                            widgetToRemove = layout.itemAt(i).widget()
                            # remove it from the layout list
                            layout.removeWidget(widgetToRemove)
                            # remove it from the gui
                            widgetToRemove.setParent(None)
            
                def create_form(self, variable_names: list):
                    self.lineEdit_out_objs = [QLineEdit() for i in range( (len(variable_names) ) )]
                    self.label_out_objs = [QLineEdit() for i in range( (len(variable_names) ) )]
                    print(self.label_outTemplate.minimumWidth())
            
                    for i in range(len(variable_names)):
                        self.formLayout_output.addRow(variable_names[i], self.lineEdit_out_objs[i])
                        
                        #config Labels proprieties here:
                        self.label_out_objs[i].setMinimumWidth(self.label_outTemplate.minimumWidth())
                        self.label_out_objs[i].setMaximumWidth(self.label_outTemplate.maximumWidth())
            
                        self.label_out_objs[i].setFixedSize(QtCore.QSize(200, 25))
            
                        #config LineEdit proprieties here:
                        self.lineEdit_out_objs[i].setMinimumWidth(self.lineEdit_outTemplate.minimumWidth())
                        self.lineEdit_out_objs[i].setMaximumWidth(self.lineEdit_outTemplate.maximumWidth())
                
                    new_height = self.scrollArea_output.viewportSizeHint().height()
                    self.scrollArea_output.resize(370,new_height+2)
            
            
            app = QtWidgets.QApplication(sys.argv)
            window = MyWindow()
            window.show()
            app.exec()
            

            and here is the code of the "interface.ui" file that I'm using made in QT Deseginer:

            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
             <class>MainWindow</class>
             <widget class="QMainWindow" name="MainWindow">
              <property name="geometry">
               <rect>
                <x>0</x>
                <y>0</y>
                <width>800</width>
                <height>600</height>
               </rect>
              </property>
              <property name="sizePolicy">
               <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="font">
               <font>
                <stylestrategy>PreferDefault</stylestrategy>
               </font>
              </property>
              <property name="windowTitle">
               <string>MainWindow</string>
              </property>
              <property name="autoFillBackground">
               <bool>false</bool>
              </property>
              <widget class="QWidget" name="centralwidget">
               <widget class="QScrollArea" name="scrollArea">
                <property name="geometry">
                 <rect>
                  <x>0</x>
                  <y>50</y>
                  <width>801</width>
                  <height>511</height>
                 </rect>
                </property>
                <property name="font">
                 <font>
                  <pointsize>12</pointsize>
                 </font>
                </property>
                <property name="widgetResizable">
                 <bool>true</bool>
                </property>
                <widget class="QWidget" name="scrollAreaWidgetContents">
                 <property name="geometry">
                  <rect>
                   <x>0</x>
                   <y>0</y>
                   <width>799</width>
                   <height>509</height>
                  </rect>
                 </property>
                 <widget class="QScrollArea" name="scrollArea_output">
                  <property name="geometry">
                   <rect>
                    <x>200</x>
                    <y>40</y>
                    <width>370</width>
                    <height>76</height>
                   </rect>
                  </property>
                  <property name="sizePolicy">
                   <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
                    <horstretch>0</horstretch>
                    <verstretch>0</verstretch>
                   </sizepolicy>
                  </property>
                  <property name="widgetResizable">
                   <bool>true</bool>
                  </property>
                  <widget class="QWidget" name="formLayout_area_out">
                   <property name="geometry">
                    <rect>
                     <x>0</x>
                     <y>0</y>
                     <width>351</width>
                     <height>74</height>
                    </rect>
                   </property>
                   <property name="sizePolicy">
                    <sizepolicy hsizetype="Fixed" vsizetype="Preferred">
                     <horstretch>0</horstretch>
                     <verstretch>0</verstretch>
                    </sizepolicy>
                   </property>
                   <layout class="QVBoxLayout" name="verticalLayout_out">
                    <property name="topMargin">
                     <number>9</number>
                    </property>
                    <item>
                     <layout class="QFormLayout" name="formLayout_output">
                      <property name="formAlignment">
                       <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
                      </property>
                      <property name="horizontalSpacing">
                       <number>6</number>
                      </property>
                      <property name="verticalSpacing">
                       <number>6</number>
                      </property>
                      <property name="leftMargin">
                       <number>0</number>
                      </property>
                      <property name="topMargin">
                       <number>0</number>
                      </property>
                      <property name="rightMargin">
                       <number>0</number>
                      </property>
                      <property name="bottomMargin">
                       <number>0</number>
                      </property>
                      <item row="0" column="0">
                       <widget class="QLabel" name="label_outTemplate">
                        <property name="sizePolicy">
                         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                          <horstretch>0</horstretch>
                          <verstretch>0</verstretch>
                         </sizepolicy>
                        </property>
                        <property name="minimumSize">
                         <size>
                          <width>200</width>
                          <height>0</height>
                         </size>
                        </property>
                        <property name="maximumSize">
                         <size>
                          <width>200</width>
                          <height>16777215</height>
                         </size>
                        </property>
                        <property name="autoFillBackground">
                         <bool>false</bool>
                        </property>
                        <property name="text">
                         <string>TextLabel:</string>
                        </property>
                       </widget>
                      </item>
                      <item row="0" column="1">
                       <widget class="QLineEdit" name="lineEdit_outTemplate">
                        <property name="sizePolicy">
                         <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
                          <horstretch>0</horstretch>
                          <verstretch>0</verstretch>
                         </sizepolicy>
                        </property>
                        <property name="minimumSize">
                         <size>
                          <width>125</width>
                          <height>0</height>
                         </size>
                        </property>
                        <property name="maximumSize">
                         <size>
                          <width>125</width>
                          <height>16777215</height>
                         </size>
                        </property>
                        <property name="text">
                         <string/>
                        </property>
                       </widget>
                      </item>
                     </layout>
                    </item>
                   </layout>
                  </widget>
                 </widget>
                </widget>
               </widget>
              </widget>
              <widget class="QMenuBar" name="menubar">
               <property name="geometry">
                <rect>
                 <x>0</x>
                 <y>0</y>
                 <width>800</width>
                 <height>21</height>
                </rect>
               </property>
              </widget>
              <widget class="QStatusBar" name="statusbar"/>
             </widget>
             <resources/>
             <connections/>
            </ui>
            
            1 Reply Last reply
            0
            • FurquimF Offline
              FurquimF Offline
              Furquim
              wrote on last edited by
              #6

              @Denni-0 ,

              Thanks for the help! I will wait for the QListTree/QListWidget version

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved