Add new row in QTreeView (Using QFileSystemModel to populate TreeView and QSortFilterProxy model to filter folder)



  • @Lifetime-Qt-Champion @Moderators @Qt-Champions-2015
    Hello,

    I am using a QFileSystemModel to populate QTreeView and QSortFilterProxyModel to filter folder so that QTreeView show only specific directory. Now I want to add a new row pointing another folder in my QTreeView.
    CODE:

    "MyProxy.h"

    class MyProxy : public QSortFilterProxyModel
    {
        Q_OBJECT
    -------------------
    };
    

    "MyProxy.cpp"

    #include "myproxy.h"
    
    MyProxy::MyProxy()
    {
    
    }
    
    bool MyProxy::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
    {
        //filter folders
    }
    

    "mainwindow.cpp"

    #include "myproxy.h"
    ................
      button=new QPushButton;
       button->setText("Click");
    
    
        connect(button,SIGNAL(clicked(bool)),this,SLOT(work()));
    
        dir=new QFileSystemModel;
        messagesTableWidget=new QTreeView;
        model=new MyProxy;    
    
    void MainWindow::work()
    {
        QString folderpath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/home",
                                                      QFileDialog::ShowDirsOnly
                                                      | QFileDialog::DontResolveSymlinks);
    
    QDir* direct = new QDir(folderpath);
    direct->setPath(folderpath);
    direct->cdUp();
    
    dir->setRootPath(direc->path());
    dir->setFilter(QDir::NoDotAndDotDot |QDir::AllDirs);
    
    model->setSourceModel(dir);
    messagesTableWidget->setModel(model);
    
         messagesTableWidget->setRootIndex(model->mapFromSource(dir->index(direc->path())));
    }
    
    

    So I am using a QFileDialog to select a folder and QDir::cdUp() for parent folder. Now I am using proxy model to filter my folder.

    Finally my treeview shows root folder. Now I want to add a row showing another folder in my Qtreeview.

    Any Suggestions?
    Thanks


  • Lifetime Qt Champion

    Hi,

    Apply the same filter you are using for your single folder version except that this time rather than using a QString you should keep a QStringList of the folders you want to show and use that.



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