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. QListView with file navigation
Qt 6.11 is out! See what's new in the release blog

QListView with file navigation

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.1k 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.
  • S Offline
    S Offline
    szumial
    wrote on last edited by
    #1

    Hi! I am trying to implement a small file browser embedded in my application GUI with the QListView widget. I have successfully managed to display the contents of a predefined folder and filter only the XML type files I am interested in. However, I'd like to give users a choice of changing the root folder from within that list view. Is it possible to add folder navigation inside the widget? What would I need to add to my listview->doubleclicked slot?

    Here's my code:

    void MeasurementModePage::setupListView()
    {
        QString protocolsPath = "D:/";
        qDebug() << QCoreApplication::applicationFilePath();
        qDebug() << QCoreApplication::applicationDirPath();
        qDebug() << protocolsPath;
        listmodel = new QFileSystemModel(this);
        listmodel->setRootPath(protocolsPath);
        QStringList filters;
        filters << "*.xml";
        listmodel->setFilter(QDir::NoDotAndDotDot | QDir::Files); // To be removed in case folders have to be also displayed
        listmodel->setNameFilters(filters);
        listmodel->setNameFilterDisables(false);
        listmodel->sort(0, Qt::AscendingOrder);
        ui->listView_measurementFiles->setModel(listmodel);
        ui->listView_measurementFiles->setRootIndex(listmodel->index(protocolsPath));
        connect(this, &MeasurementModePage::settingsChanged, this, &MeasurementModePage::onSettingsLoad);
    
        // Context menu
        ui->listView_measurementFiles->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(ui->listView_measurementFiles, &QWidget::customContextMenuRequested, this, &MeasurementModePage:: showCustomContextMenu);
    }
    
    void MeasurementModePage::on_listView_measurementFiles_doubleClicked(const QModelIndex &index)
    {
        // Load an XML settings file if the index selected is a file
        if(listmodel->fileInfo(index).isFile())
        {
            // Load settings with external LIB
            emit settingsChanged();
        }
    }
    

    On a side note: I would like the root path to be set to a folder where my application is built, where I will place some XML files for the users to choose from. Does it make sense to read it using "QCoreApplication::applicationDirPath();"?

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

      Hi,

      It depends on the target platform and how you will install your application.

      How far would your users be allowed to roam for these xml files ?

      Depending on that, a tree view might be better suited.

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

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        It depends on the target platform and how you will install your application.

        How far would your users be allowed to roam for these xml files ?

        Depending on that, a tree view might be better suited.

        S Offline
        S Offline
        szumial
        wrote on last edited by
        #3

        @SGaist I am thinking to give users a freedom of choice. The application would always launch showing the default root path, but they should be allowed to choose any other directory. I will only do the filtering and display XML files exclusively. I'll try with a tree view today and see if this achieves that goal.

        Regarding the applitacion directory - the target platform is Windows and for now, the application will come without an installer - just a compiled version to work with.

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

          @szumial said in QListView with file navigation:

          Regarding the applitacion directory - the target platform is Windows and for now, the application will come without an installer - just a compiled version to work with.

          Then you should be good.

          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
          • S Offline
            S Offline
            szumial
            wrote on last edited by
            #5

            I ended up implementing a QListView and added a tool button to allow users select any directory with a QFileDialog. Since I intend to make the program easy to use, I don't want them to change the root path frequently. I will place the XML files in a specific directory and the ability to change the folder displayed is just an additional functionality. Thanks for the suggestions!

            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