Home > Mobile >  PyQt5 - Centre objects in QGridLayout
PyQt5 - Centre objects in QGridLayout

Time:06-09

I have a question. I have tried layouts in PyQt5 and it works partially. But not the way I want it.

This is my code:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class CompanyLogin(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()

        self.layout = QGridLayout()

        self.setLayout(self.layout)
        self.setWindowTitle('Window')
        self.setGeometry(0, 0, 1920, 1080)

        self.setupUI()

    def setupUI(self):
        company_note = QLabel(parent=self, text='Company: ')
        self.company = QComboBox(parent=self)
        self.company.addItem('t1')
        self.company.addItem('t2')

        password_note = QLabel(parent=self, text='Password: ')
        self.password = QLineEdit(parent=self)

        submit = QPushButton(parent=self, text='Submit')

        verticalSpacer = QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.layout.addItem(verticalSpacer, 6, 0, Qt.AlignTop)

        self.layout.addWidget(company_note, 1, 0, Qt.AlignLeft)
        self.layout.addWidget(self.company, 1, 1, Qt.AlignRight)

        self.layout.addWidget(password_note, 2, 0, Qt.AlignLeft)
        self.layout.addWidget(self.password, 2, 1, Qt.AlignRight)

        self.layout.addWidget(submit, 3, 0, 1, 0, Qt.AlignCenter)

This is what my window looks like so far:

Window Now

And this is how I want it to look:

Window After

CodePudding user response:

Right now the simplest solution could be to add this simple line:

self.layout.setAlignment(Qt.AlignTop|Qt.AlignHCenter)

But, actually, setting the layout alignment is often cause of unexpected problems, and the fact is that there are some issues in your code that should be fixed instead:

  • you are adding widgets starting from the second row;
  • you are using alignments when adding widgets, but that only ensures that the widget minimum size hint will be used, and the widget will then follow the alignment, which is clearly not what you need;
  • a 0 value span as you did in the last line is wrong, since it indicates how many row or column cells a widget occupies (it's not index based), so it should be 1 or more;
  • you should use setRowStretch() and setColumnStretch();

Other small notes: layout() is an existing dynamic function of all QWidgets, you shall not overwrite it; also, the parent argument in widget constructors is pointless if you then add the widget to the parent's layout; finally, use keyword arguments only when required.

    def setupUI(self):
        self.layout.setColumnStretch(0, 1)
        self.layout.setColumnStretch(3, 1)
        self.layout.setRowStretch(3, 1)

        company_note = QLabel('Company: ')
        self.company = QComboBox()
        self.company.addItem('t1')
        self.company.addItem('t2')

        password_note = QLabel('Password: ')
        self.password = QLineEdit()

        submit = QPushButton('Submit')

        self.layout.addWidget(company_note, 0, 1)
        self.layout.addWidget(self.company, 0, 2)

        self.layout.addWidget(password_note, 1, 1)
        self.layout.addWidget(self.password, 1, 2)

        self.layout.addWidget(submit, 2, 1, 1, 2)
  • Related