Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    qt5.6 qtreeview qfilesystemmode qsortfilterprox row
    2
    2
    1782
    Loading More Posts
    • 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.
    • Punit
      Punit last edited by Punit

      @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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • First post
          Last post