如何从QFileSystemModel中填充几个QComboBox?

2024-04-19 22:54:51 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用QFileSystemModel用子目录填充几个QComboBox?你知道吗

我已经建立了一个项目管理工具,允许我创建和管理我的项目。我目前正在使用操作系统列表目录和json来填充和验证我的qcombobox。但我正在尝试学习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,但是在我的代码中它似乎不起作用-它只是保持为空。你知道吗


Tags: selfmodelinitdefrootwindowclassparent
1条回答
网友
1楼 · 发布于 2024-04-19 22:54:51

好的,这是你的修改版本:

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

相关问题 更多 >