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. File sorting and selection
Forum Updated to NodeBB v4.3 + New Features

File sorting and selection

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    foolah44
    wrote on last edited by
    #1

    Hi everyone.

    I have recently started using Qt. I want to create a window containing a list of files in a pre selected folder.
    Also, in that window, i want to include three radio button: by name, by type and by folder. Each of the radio button will have to sort the file list accordingly.

    I understand this has to do with Qt Model/View. But I have so much trouble finding the appropriate model for this.
    I dont really see how to sort the file list when a radio button is checked.

    Thank you.

    This is my code to populate the list so far:

    void batchWindow::populateTable()
    {

    QDir directory(folderName);
    fileList = directory.entryList();
    QStringListModel *model = new QStringListModel(fileList);
    elements->setModel(model);
    

    }

    But it only gives a list of the main folder, not sub folders.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QTDavid
      wrote on last edited by
      #2

      You should design a function that gets folderName. The code you gave is only populating the table once, you have to re-populate it to show files and folders in a different directory.
      Something that does the following when a file is selected from the list:

      (a bit of pseudocode)

      @
      if (selection is a folder) {
      return name of folder

      } else {
      // deal with opening file or else

      }
      @

      and call your populateTable() again with a different folderName.
      Personally, considered the case, I would add an argument to populateTable()

      @populateTable(QString folderName)@

      (is it supposed to be a QString or QDir?)

      I think that is how you could do it, but you surely have to change the folderName to update the list.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        foolah44
        wrote on last edited by
        #3

        QTDavid,

        folderName is a private member of the class batchwindow
        I didn't find necessary to add it as an argument to populateTable()

        I have changed my model to QFileSystemModel and it works better now:

        @
        void batchWindow::populateTable()
        {
        QDir directory(folderName);
        fileList = directory.entryList();

        QFileSystemModel *model = new QFileSystemModel;
        model->removeColumn(3);
        elements->setModel(model);
        model->setRootPath(folderName);
        elements->setRootIndex(model->index(folderName));
        elements->hideColumn(3);
        

        }
        @

        I have also created a slot sortTable() that runs when a radio button is checked.

        @
        void batchWindow::sortTable()
        {
        QDir directory(folderName);
        fileList = directory.entryList();

        if (byFolder->isChecked()){
        
            QFileSystemModel *model = new QFileSystemModel;
            elements->setModel(model);
            model->setRootPath(folderName);
            elements->setRootIndex(model->index(folderName));
            elements->hideColumn(3);
        }
        if (byName->isChecked()){
        
        }
        

        }
        @

        It is a bit repetive but it works for the moment. I am now trying to figure out how to sort by name and by type.

        1 Reply Last reply
        0

        • Login

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