Yuval Parness
Yuval Parness

Reputation: 29

How can I set the sizePolicy of my mainWindow?

I made a simple mainWindow with the help of the qt designer and got the whole code using the pyuic5 option in the cmd. I added this lineto the main part:

QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

to be able to see my window correctly (without this its a whole mass).

In the properties inside qt designer i changed the sizePolicy values to be expanding, but when i try to run my application the sizePolicy is not working, it all stays in the left upper corner.

This is the code i got from the pyuic5:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_CompanyCommanderLogin(object):
    def setupUi(self, CompanyCommanderLogin):
        CompanyCommanderLogin.setObjectName("CompanyCommanderLogin")
        CompanyCommanderLogin.resize(252, 227)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(CompanyCommanderLogin.sizePolicy().hasHeightForWidth())
        CompanyCommanderLogin.setSizePolicy(sizePolicy)
        self.centralwidget = QtWidgets.QWidget(CompanyCommanderLogin)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 10, 231, 171))
        self.groupBox.setObjectName("groupBox")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(20, 40, 71, 16))
        self.label.setObjectName("label")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(20, 70, 41, 16))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.groupBox)
        self.label_4.setGeometry(QtCore.QRect(20, 100, 41, 16))
        self.label_4.setObjectName("label_4")
        self.lineEdit_x_location = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit_x_location.setGeometry(QtCore.QRect(100, 70, 51, 20))
        self.lineEdit_x_location.setObjectName("lineEdit_x_location")
        self.lineEdit_y_location = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit_y_location.setGeometry(QtCore.QRect(100, 100, 51, 20))
        self.lineEdit_y_location.setObjectName("lineEdit_y_location")
        self.pushButton_login = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_login.setGeometry(QtCore.QRect(140, 140, 71, 21))
        self.pushButton_login.setStyleSheet("background-color: rgb(0, 209, 255);\n"
"color: rgb(255, 255, 255);\n"
"background-color: rgb(0, 0, 127);")
        self.pushButton_login.setObjectName("pushButton_login")
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setGeometry(QtCore.QRect(100, 40, 51, 21))
        self.comboBox.setObjectName("comboBox")
        CompanyCommanderLogin.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(CompanyCommanderLogin)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 252, 18))
        self.menubar.setObjectName("menubar")
        CompanyCommanderLogin.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(CompanyCommanderLogin)
        self.statusbar.setObjectName("statusbar")
        CompanyCommanderLogin.setStatusBar(self.statusbar)

        self.retranslateUi(CompanyCommanderLogin)
        QtCore.QMetaObject.connectSlotsByName(CompanyCommanderLogin)

    def retranslateUi(self, CompanyCommanderLogin):
        _translate = QtCore.QCoreApplication.translate
        CompanyCommanderLogin.setWindowTitle(_translate("CompanyCommanderLogin", "Company Commander Login"))
        self.groupBox.setTitle(_translate("CompanyCommanderLogin", "Login"))
        self.label.setText(_translate("CompanyCommanderLogin", "<html><head/><body><p><span style=\" font-size:9pt;\">Company Number:</span></p></body></html>"))
        self.label_3.setText(_translate("CompanyCommanderLogin", "<html><head/><body><p><span style=\" font-size:9pt;\">X Location:</span></p></body></html>"))
        self.label_4.setText(_translate("CompanyCommanderLogin", "<html><head/><body><p><span style=\" font-size:9pt;\">Y Location:</span></p></body></html>"))
        self.pushButton_login.setText(_translate("CompanyCommanderLogin", "Login"))


if __name__ == "__main__":
    import sys
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QtWidgets.QApplication(sys.argv)
    CompanyCommanderLogin = QtWidgets.QMainWindow()
    ui = Ui_CompanyCommanderLogin()
    ui.setupUi(CompanyCommanderLogin)
    CompanyCommanderLogin.show()
    sys.exit(app.exec_())

What could be the problem with the sizePolicy? Is there another way to make my mainWindow the expand?

Picture for example (Here the login is in the left upper corner, I want it to be the whole window size)

enter image description here

Upvotes: 2

Views: 13140

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

As the docs points out:

The size policy of a widget is an expression of its willingness to be resized in various ways, and affects how the widget is treated by the layout engine. Each widget returns a QSizePolicy that describes the horizontal and vertical resizing policy it prefers when being laid out. You can change this for a specific widget by changing its QWidget::sizePolicy property.

The QSizePolicy work only in layouts, and in your case there are none.

So the solution is to create your design using the QXLayout, if you want to use it in Qt Designer you should read the Qt Designer Manual and especially section Using Layouts in Qt Designer.

In this case, since you have not provided the .ui that would allow me to use Qt Designer, I will quickly provide the solution from scratch

from PyQt5 import QtCore, QtGui, QtWidgets


class CompanyCommanderLogin(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.groupbox = QtWidgets.QGroupBox(self.tr("Login"))
        self.company_cb = QtWidgets.QComboBox()
        sp = self.company_cb.sizePolicy()
        sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
        self.company_cb.setSizePolicy(sp)
        self.xlocation_le = QtWidgets.QLineEdit()
        self.ylocation_le = QtWidgets.QLineEdit()
        self.login_button = QtWidgets.QPushButton(self.tr("Login"))
        self.login_button.setStyleSheet(
            "background-color: rgb(0, 209, 255);\n"
            "color: rgb(255, 255, 255);\n"
            "background-color: rgb(0, 0, 127);"
        )

        font = self.font()
        font.setPointSize(9)
        self.setFont(font)

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.groupbox)

        flay = QtWidgets.QFormLayout()

        flay.addRow(
            self.tr("Company Number:"), self.company_cb,
        )
        flay.addRow(
            self.tr("X Location:"), self.xlocation_le,
        )
        flay.addRow(
            self.tr("Y Location:"), self.ylocation_le,
        )

        lay = QtWidgets.QVBoxLayout()
        lay.addLayout(flay)
        lay.addWidget(self.login_button, alignment=QtCore.Qt.AlignHCenter)
        lay.addStretch()
        self.groupbox.setLayout(lay)


if __name__ == "__main__":
    import sys

    if hasattr(QtCore.Qt, "AA_EnableHighDpiScaling"):
        QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

    app = QtWidgets.QApplication(sys.argv)
    w = CompanyCommanderLogin()
    w.show()
    sys.exit(app.exec_())

Upvotes: 3

Related Questions