Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从QFileSystemModel填充多个QComboBox?_Python_Pyqt - Fatal编程技术网

Python 如何从QFileSystemModel填充多个QComboBox?

Python 如何从QFileSystemModel填充多个QComboBox?,python,pyqt,Python,Pyqt,如何使用QFileSystemModel用子目录填充多个QComboBox 我已经建立了一个项目管理工具,允许我创建和管理我的项目。我目前正在使用os.listdir和json的组合来填充和验证我的QComboxes。但我正试图通过QFileSystemModel学习更多的modelview方法 这就是我所拥有的: class FileSystemModel(QW.QFileSystemModel): def __init__(self, root, parent=None):

如何使用QFileSystemModel用子目录填充多个QComboBox

我已经建立了一个项目管理工具,允许我创建和管理我的项目。我目前正在使用os.listdir和json的组合来填充和验证我的QComboxes。但我正试图通过QFileSystemModel学习更多的modelview方法

这就是我所拥有的:

class FileSystemModel(QW.QFileSystemModel):
    def __init__(self, root, parent=None):
        QW.QFileSystemModel.__init__(self, parent)
        self.root = root
        self.rootIndex = self.setRootPath(root)

class Window(QW.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__()
        self.init()

    def init(self):
        layout = QW.QVBoxLayout()

        self.cbox = QW.QComboBox()
        self.cbox2 = QW.QComboBox()

        self.model = FileSystemModel("C:\\projects\\")
        self.cbox.setModel(self.model)
        self.cbox2.setModel(self.model)
        self.cbox.setRootModelIndex(self.model.rootIndex)

        self.cbox.currentIndexChanged.connect(self._indexChanged)

        layout.addWidget(self.cbox)
        layout.addWidget(self.cbox2)
        self.setLayout(layout)

    def _indexChanged(self):
        row = self.sender().currentIndex()
        index = self.sender().rootModelIndex().child(row, 0)
        self.cbox2.setRootModelIndex(index)


def main():
    app = QW.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

我试图使用cbox中的索引重新填充cbox2,但我的代码似乎不起作用-它只是保持为空。

好的,这里是您所拥有的修改版本:

from sys import exit as sysExit

from PyQt5.QtCore    import QDir, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QFileSystemModel, QHBoxLayout, QComboBox

class SysDirModel(QFileSystemModel):
    def __init__(self, DirPath):
        QFileSystemModel.__init__(self)

        self.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)
        self.setReadOnly(True)
      # Property
        self.setRootPath(DirPath)
      # Property
        self.RootIndex = self.index(DirPath)

class SysFileModel(QFileSystemModel):
    def __init__(self, DirPath):
        QFileSystemModel.__init__(self)

        self.setFilter(QDir.NoDotAndDotDot | QDir.Files)
        self.setReadOnly(True)
      # Property
        self.setRootPath(DirPath)
      # Property
        self.RootIndex = self.index(DirPath)

    def ResetPath(self, DirPath):
        self.setRootPath(DirPath)
        self.RootIndex = self.index(DirPath)


class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.setGeometry(150, 150, 450, 100)

      # If you use forward slash this works in Windows as well and it is cleaner
        self.SysDirs = SysDirModel('C:/projects/')
        self.SysFils = SysFileModel('C:/projects/')
      # Setup first ComboBox
        self.cbxDirs = QComboBox()
        self.cbxDirs.setMinimumWidth(200)
        self.cbxDirs.setModel(self.SysDirs)
        self.cbxDirs.setRootModelIndex(self.SysDirs.RootIndex)
      # This sends a Signal to a predefined Slot
        self.cbxDirs.currentIndexChanged.connect(self.IndexChanged)

        self.cbxFiles = QComboBox()
        self.cbxFiles.setMinimumWidth(200)
        self.cbxFiles.setModel(self.SysFils)
        self.cbxFiles.setRootModelIndex(self.SysFils.RootIndex)

        HBox = QHBoxLayout()
        HBox.addWidget(self.cbxDirs)
        HBox.addStretch(1)
        HBox.addWidget(self.cbxFiles)

        self.setLayout(HBox)

  # This is the receiver of a Signal (aka Slot) so it ought to be used as such
    @pyqtSlot(int)
    def IndexChanged(self, RowIdx):
      # Get your Current DirPath based on the Selected Value
        index = self.cbxDirs.rootModelIndex().child(RowIdx, 0)
        DirPath = self.cbxDirs.model().filePath(index)
      # Reset what ComboBox 2's Model and what it is looking at 
        self.cbxFiles.clear()
        self.SysFils.ResetPath(DirPath)
        self.cbxFiles.setModel(self.SysFils)


if __name__ == '__main__':
    MainThred = QApplication([])

    MainGui = MainWindow()
    MainGui.show()

    sysExit(MainThred.exec_())

谢谢你的评论。所以没有办法在多个视图中分割模型数据?我需要为每个组合框实现一个单独的模型?最后,我将有4个组合框,每个组合框显示树的不同分支。您可以将类设计为更通用的,然后在外部将过滤器应用于它,但我不理解您的意思,即有4个组合框,每个组合框显示树的不同分支?你是如何从一个显示目录和一个显示文件(只有2个组合框)到4个组合框的?你在说什么其他分支?还有为什么你不使用树状视图?嗨,丹尼斯,再次感谢你的评论。所以我认为我没有正确地解释我的问题。我知道FileSystemModel最常用的用法是在一个视图中显示文件夹,在另一个视图中显示文件。但是,我只想显示文件夹,类似于树,但表示为树的每个分支的组合框。这纯粹是一种设计选择。没有办法避免需要使用单独的过滤器,但正如我所说的,可以使上述两个类更通用——比如传入一个变量并在if语句中设置过滤器——或者类似的东西,但我不确定我是否会这样做——我的意思是有4个单独的类对于这4个独立的对象,意味着如果您想更改其中一个对象中的某些内容,那么无论如何,您都不会影响其他3个对象,但这完全取决于您