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. Add new row in QTreeView (Using QFileSystemModel to populate TreeView and QSortFilterProxy model to filter folder)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5.6qtreeviewqfilesystemmodeqsortfilterproxrow
2 Posts 2 Posters 2.1k 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.
  • PunitP Offline
    PunitP Offline
    Punit
    wrote on last edited by Punit
    #1

    @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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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