Problem with QtFormLayout
-
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):
But evey time I remove the first line it stays like this:
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:
Ps: Im using Python 3.8.2 and PyQt5 5.13.2Thanks for your attention!
-
Hi,
You should be able to get what you want using both formAlignment and labelAlignment.
-
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)
-
Then please provide a complete minimal example to allow testing.
-
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>