我在qt设计器的帮助下制作了一个简单的mainWindow,并使用cmd中的pyuic5选项获得了完整的代码。 我将此行添加到主要部分:
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
以便能够正确看到我的窗口(没有整个窗口)。
在qt designer内的属性中,我更改了sizePolicy值以进行扩展,但是当我尝试运行我的应用程序时,sizePolicy无法正常工作,它全部留在了左上角。
这是我从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_())
sizePolicy可能是什么问题?有没有其他方法可以使mainWindow扩展?
例如图片(此处登录位于左上角,我希望它是整个窗口的大小)
答案 0 :(得分:1)
the docs指出:
小部件的大小策略是其以各种方式调整大小的意愿的表达,并且会影响layout engine对小部件的处理方式。每个窗口小部件都返回一个QSizePolicy,该页面描述了布局时首选的水平和垂直大小调整策略。您可以通过更改特定窗口小部件的QWidget::sizePolicy属性来对其进行更改。
QSizePolicy仅在布局中起作用,而在您的情况下则没有。
因此解决方案是使用QXLayout创建设计,如果要在Qt Designer中使用它,则应阅读Qt Designer Manual,尤其是Using Layouts in Qt Designer部分。
在这种情况下,由于您没有提供允许我使用Qt Designer的.ui,因此我将迅速从头开始提供解决方案
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_())