Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Default item in QComboBox w/ QFileSystemModel
Qt 6.11 is out! See what's new in the release blog

Default item in QComboBox w/ QFileSystemModel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wallboy
    wrote on last edited by VRonin
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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) 
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      W 1 Reply Last reply
      2
      • VRoninV VRonin

        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) 
        
        W Offline
        W Offline
        Wallboy
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wallboy
          wrote on last edited by Wallboy
          #4

          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))
          
          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved