【问题标题】:How to change the size of PyQt5 directory view in the main window?如何在主窗口中更改 PyQt5 目录视图的大小?
【发布时间】:2018-11-08 20:03:39
【问题描述】:

我正在开发一个 PyQt5 项目,该项目需要 PyQt5 QTreeView 的文件夹查看器。为了放更多的东西,我尝试改变树视图的大小,但徒劳无功。这是来自 Pythonspot 的代码:

import sys
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
from PyQt5.QtGui import QIcon

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 file system view - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.model = QFileSystemModel()
        self.model.setRootPath('')
        self.tree = QTreeView()
        self.tree.setModel(self.model)

        self.tree.setAnimated(False)
        self.tree.setIndentation(20)
        self.tree.setSortingEnabled(True)

        self.tree.setWindowTitle("Dir View")
        self.tree.resize(640, 200)

        windowLayout = QVBoxLayout()
        windowLayout.addWidget(self.tree)
        self.setLayout(windowLayout)

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

我通过

更改树视图
self.tree.resize(640, 200)

为什么它不起作用?

【问题讨论】:

    标签: python python-3.x pyqt5 qtreeview


    【解决方案1】:

    布局用于确定您正在使用的小部件的位置和大小,因此在您的情况下,即使您使用调整大小,大小也不会改变,而是应该设置一个固定大小,这样布局就不会改变QTreeView 的大小。

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class App(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 file system view - pythonspot.com'
            self.left, self.top, self.width, self.height = 10, 10, 640, 480
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            self.model = QtWidgets.QFileSystemModel()
            self.model.setRootPath('')
            self.tree = QtWidgets.QTreeView()
            self.tree.setModel(self.model)
    
            self.tree.setAnimated(False)
            self.tree.setIndentation(20)
            self.tree.setSortingEnabled(True)
    
            self.tree.setWindowTitle("Dir View")
            self.tree.setFixedSize(640, 200)
    
            windowLayout = QtWidgets.QVBoxLayout(self)
            windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)
    
            self.show()
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    

    【讨论】:

    • 谢谢。现在我明白了
    • 如果我想通过以下行移动树视图? self.tree.move(200,100)
    • @TimChen 然后不要使用布局,因为它消除了:windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) 并将self.tree = QtWidgets.QTreeView() 更改为self.tree = QtWidgets.QTreeView(self) 并将self.tree.setFixedSize(640, 200) 更改为self.tree.setGeometry(200, 100, 640, 200)
    • 感谢您的帮助!如果我想在目录视图中双击一个文件后弹出一个窗口,我可以在 initUI(self) 中添加一行:itemDoubleClicked.connect(self.buildExamplePopup)。并在下面添加一个函数:def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    相关资源
    最近更新 更多