我正在一个项目中,我在qt designer
中创建UI,并在python
中编写其代码。我尚未在qt设计器中设置ui小部件或窗口的任何高度和宽度属性。当我启动它时,它没有覆盖整个窗口。下面是代码:
app.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.test import Ui_MainWindow
class Login(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
app = QApplication(sys.argv)
main_window = Login()
main_window.show()
sys.exit(app.exec_())
test.py (已从test.ui转换而来)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1177, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.table_widget = QtWidgets.QTableWidget(self.centralwidget)
self.table_widget.setGeometry(QtCore.QRect(10, 10, 1161, 421))
self.table_widget.setRowCount(20)
self.table_widget.setColumnCount(20)
self.table_widget.setObjectName("table_widget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
启动代码时,如下所示:
在上图中,我可以移动滚动条以查看所有行和列,但是如果单击最大化窗口,则如下所示:
但是在上面的图像中,高度和宽度保持不变。我如何使其全屏显示,以使窗口小部件的高度和宽度与窗口大小匹配。请帮忙。谢谢
答案 0 :(得分:0)
您忘记将table_widget添加到centralwidget中,因此您应该使用布局。
以下是有关Layout Management的信息。您还可以在Qt Designer(Using Layouts in Qt Designer)中设置布局。
layout = QVBoxLayout()
layout.addWidget(self.table_widget)
self.centralwidget.setLayout(layout)
整个代码。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QVBoxLayout
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1177, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.table_widget = QtWidgets.QTableWidget(self.centralwidget)
# adding table_widget to central widget
layout = QVBoxLayout()
layout.addWidget(self.table_widget)
self.centralwidget.setLayout(layout)
# end
self.table_widget.setGeometry(QtCore.QRect(10, 10, 1161, 421))
self.table_widget.setRowCount(20)
self.table_widget.setColumnCount(20)
self.table_widget.setObjectName("table_widget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())