Default item in QComboBox w/ QFileSystemModel



  • I have a QComboBox that has it's model set to a QFileSystemModel that displays logo files from a directory that you can choose from. I want to make the first item (index 0) in the QComboBox a simple "None" string.

    I currently have something like this (PyQt):

    logoPath = QDir.currentPath()+'/logos/'
    self.logoModel = QFileSystemModel(self)
    self.logoModel.setRootPath(logoPath)
    self.logoModel.setFilter(QDir.Files)
    self.logoComboBox.setModel(self.logoModel)
    self.logoComboBox.setRootModelIndex(self.logoModel.index(logoPath))
    
    self.logoComboBox.insertItem(0, 'None')  # Doesn't work
    self.logoComboBox.setCurrentIndex(0) 
    

    The insertItem doesn't do anything, and I can only see the files in the QComboBox

    Any help would be appreciated,

    Thanks



  • The smart way:

    • create a model that contains just the "None" auto noneModel = new QStringListModel(QStringList({"None"}),this);
    • concatenate the two models with KConcatenateRowsProxyModel

    The quick and dirty way:

    logoPath = QDir.currentPath()+'/logos/'
    self.logoModel = QFileSystemModel(self)
    self.logoModel.setRootPath(logoPath)
    self.logoModel.setFilter(QDir.Files)
    self.logoComboBox.addItem('None')
    for i in xrange(0,self.logoModel.rowCount(self.logoModel.index(logoPath))):
        self.logoComboBox.addItem(self.logoModel.index(i,0,self.logoModel.index(logoPath)).data().toString());
    self.logoComboBox.setCurrentIndex(0) 
    


  • @VRonin

    Trying the "quick and dirty way" and for some reason the following is returning 0 rows

    self.logoModel.rowCount(self.logoModel.index(logoPath))
    

    Usually something like "---Choose---" is a common thing to have in combo boxes, is it not?. Which Qt developer does one have to yell at to add what seems such an obvious feature that the QComboBox class is missing lol.

    Is concatenating with a proxy model really the simplest correct way? I'd have to go way out of the way to port that concatenate rows model to Python.

    Going to stick with your idea of looping through each file and adding it to the ComboBox. I just need to figure out why rowCount() is returning 0.



  • Alright I figured it out. QFileSystemModel uses deferred loading so you have to wait till it's finished loading the directory structure using the directoryLoaded signal before running the loop:

    logoPath = QDir.currentPath()+'/logos/'
    self.logoModel = QFileSystemModel(self)
    self.logoModel.setRootPath(logoPath)
    self.logoModel.setFilter(QDir.Files | QDir.NoDotAndDotDot)
    self.logoComboBox.addItem('None')
    self.logoModel.directoryLoaded.connect(self.fillLogoComboBox)
    
    def fillLogoComboBox(self, path):
        for i in range(0, self.logoModel.rowCount(self.logoModel.index(path))):
            logoIdx = self.logoModel.index(i, 0, self.logoModel.index(path))
            self.logoComboBox.addItem(logoIdx.data(), self.logoModel.filePath(logoIdx))
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.