Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

File sorting and selection



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



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



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


Log in to reply