PYQT5窗口仅显示部分视图

时间:2018-11-19 21:41:06

标签: python pyqt5

我正在使用pyqt5窗口,但是由于某种原因,该窗口仅显示了我想要的图像的局部视图, 我的代码有什么问题?

我试图调整窗口大小,但仍然可以看到部分视图。

任何人都可以帮忙吗?enter image description here

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(300, 500)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")


        self.listview = QtWidgets.QListView(self.centralwidget)
        self.listview.setGeometry(QtCore.QRect(10,10,200,300))
        self.listview.setObjectName("listview")

        path = r'\\user\folder' 

        self.fileModel = QtWidgets.QFileSystemModel(self.centralwidget)
        self.fileModel.setRootPath(path)
        self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.listview.setModel(self.fileModel)
        self.listview.setRootIndex(self.fileModel.index(path))

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20,220,100,20))
        self.label.setObjectName("label")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setGeometry(QtCore.QRect(20,250,100,20))



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


    def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "App"))
         self.label.setText(_translate("MainWindow", "table for complete data?"))
         self.pushButton.setText(_translate("MainWindow", "Get data"))
         MainWindow.show()

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_())

2 个答案:

答案 0 :(得分:0)

您需要管理窗口的布局。使用QGridLayout可以很容易地做到这一点:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QGridLayout

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(300, 500)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.listview = QtWidgets.QListView(self.centralwidget)
        self.listview.setObjectName("listview")
        path = r'\\user\folder'

        self.fileModel = QtWidgets.QFileSystemModel(self.centralwidget)
        self.fileModel.setRootPath(path)
        self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.listview.setModel(self.fileModel)
        self.listview.setRootIndex(self.fileModel.index(path))

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")

        # Adding a GridLayout (1 column), adding the widgets and sets a geometry. 
        grid = QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(self.listview, 1, 0)
        grid.addWidget(self.label, 2, 0)
        grid.addWidget(self.pushButton, 3, 0)
        self.centralwidget.setLayout(grid)
        self.centralwidget.setGeometry(300,300,350,350)

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


    def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "App"))
         self.label.setText(_translate("MainWindow", "table for complete data?"))
         self.pushButton.setText(_translate("MainWindow", "Get data"))
         MainWindow.show()

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_())

结果如下:

enter image description here

答案 1 :(得分:0)

看到的原因是由于将窗口小部件设置为子代时,并不表示他知道子代的大小,例如在您的情况下self.centralwidget是MainWindow的子代,但是MainWindow不知道该子代的大小。 self.centralwidget asu self的大小。 centralwidget的大小由sizeHint设置。另一方面,如果父亲的大小合适,则只有孩子可以看见,而在您的情况下,self.centralwidget很小,以至于您的孩子看不到。

在这种情况下,建议使用qlayout,但基本的操作是将self.centralwidget设置为QMainWindow的centralWidget,QMainWindow是已经具有qlayout的特殊控件。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(300, 500)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.listview = QtWidgets.QListView(self.centralwidget)
        self.listview.setGeometry(QtCore.QRect(10,10,200,300))
        self.listview.setObjectName("listview")

        path = r'\\user\folder' 

        self.fileModel = QtWidgets.QFileSystemModel(self.centralwidget)
        self.fileModel.setRootPath(path)
        self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.listview.setModel(self.fileModel)
        self.listview.setRootIndex(self.fileModel.index(path))

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20,220,100,20))
        self.label.setObjectName("label")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setGeometry(QtCore.QRect(20,250,100,20))

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


    def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "App"))
         self.label.setText(_translate("MainWindow", "table for complete data?"))
         self.pushButton.setText(_translate("MainWindow", "Get data"))
         MainWindow.show()

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_())

enter image description here

尽管更好的选择是使用布局并改善您的代码,但我们获得:

from PyQt5 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("App")
        self.centralwidget = QtWidgets.QWidget()
        self.setCentralWidget(self.centralwidget)

        self.listview = QtWidgets.QListView()
        self.label = QtWidgets.QLabel("table for complete data?")
        self.pushButton = QtWidgets.QPushButton("Get data")
        path = r'\\user\folder' 
        self.fileModel = QtWidgets.QFileSystemModel(self)
        self.fileModel.setRootPath(path)
        self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.listview.setModel(self.fileModel)
        self.listview.setRootIndex(self.fileModel.index(path))

        lay = QtWidgets.QVBoxLayout(self.centralwidget)
        lay.addWidget(self.listview)
        lay.addWidget(self.label)
        lay.addWidget(self.pushButton)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here