Skip to content

CopperSpice Journal

CopperSpice Journal

Source Code  (Gui Example 25)

Posted on June 7, 2022June 8, 2022 By Barbara Geller

This example uses a QTreeView with the QFileSystemModel to display the structure of a file system.

#include <QtCore>
#include <QtGui>

class MainWindow : public QWidget
{
 public:
   MainWindow();

 private:
   QTreeView *m_view;
};

This declaration for MainWindow declares one data member as a pointer to a QTreeView.

MainWindow::MainWindow()
{
   QString path;

#ifdef Q_OS_WIN
   QString currentPath = QDir::currentPath();
   path = currentPath.left(currentPath.indexOf('/') + 1);
#else
   path = QDir::homePath();
#endif

   setMinimumSize(700, 500);

   QLabel *label = new QLabel();
   label->setText("File System Tree View\nRoot Path: " + path);
   label->setAlignment(Qt::AlignHCenter);

   QFont font = label->font();
   font.setPointSize(11);
   label->setFont(font);

   QFileSystemModel *model = new QFileSystemModel();

   model->setFilter(
      QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files);

   model->setRootPath(path);

   m_view = new QTreeView();
   m_view->setModel(model);
   m_view->setRootIndex(model->index(path));
   m_view->setIndentation(25);

   m_view->setSortingEnabled(true);
   m_view->sortByColumn(0, Qt::AscendingOrder);
   m_view->header()->
      setSectionResizeMode(QHeaderView::ResizeToContents);

   QPushButton *pb_close = new QPushButton();
   pb_close->setText("Close");

   QHBoxLayout *layout = new QHBoxLayout();
   layout->addStretch();
   layout->addWidget(pb_close);
   layout->addStretch();

   QVBoxLayout *layout_main = new QVBoxLayout(this);
   layout_main->setContentsMargins(20, 20, 20, 15);

   layout_main->addWidget(label);
   layout_main->addSpacing(10);
   layout_main->addWidget(m_view);
   layout_main->addSpacing(10);
   layout_main->addLayout(layout);

   connect(pb_close, &QPushButton::clicked, 
      this, &QWidget::close);
}

As in our previous three examples a model class is used to store the data so it is separate from the view. This design conforms to the Model/View paradigm. For more information refer to the Model/View Architecture in our API documentation. The QDirModel is an older class which is slower and does not offer as many features. We will be using QFileSystemModel for increased performance and functionality.

The first part of this code sets up the path where the model will start reading the file system. The #ifdef on line 5 allows us to assign a different value based on the operating system. For Windows we want to start at the root of the current drive. On Unix and Mac we want to start from the users home directory.

On line 22 the file system model is constructed and the next line sets a filter indicating what types of files to display. There are several other enum values in QDir which can filter based on values like symlinks and permissions. The model will not be populated until the call to setRootPath() is made on line 27. This prevents unnecessary queries to the file system. If you pass an empty string the model will read the file system starting at root. On Windows this would be the C drive.

The tree view is set up starting on line 29. The call to setRootIndex() is required in our example since we want to control where the view will start displaying from. The default tree indentation is a size of 20. On line 32 we increased this to 25 for just for fun.

The call to setSortingEnabled(true) on line 34 does multiple things for us. It makes another call to set the sort indicator on the column the view is sorted on and it makes the column headers clickable. Line 35 instructs the view to sort based on the file name in column 0 in Ascending order.

The last line of tree view code is on line 37 where we tell the view to adjust the size of the columns based on the contents. This is very convenient for a view like a file system where guessing would be difficult. The remainder of the code simply lays out the widgets on the window.

Main Function

Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.

Running the Example

To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_25”.

https://download.copperspice.com/journal/example_25.zip

Uncategorized

Post navigation

Previous Post: Source Code  (Gui Example 24)
Next Post: Source Code  (Gui Example 26)
  • CopperSpice Journal Homepage
    • Table of Contents
    • Example Source Code
    • Discussion Forum
  • CopperSpice Homepage
  • Github Repository
  • Videos About C++

Post comments or questions on our CopperSpice Forum

Copyright © 2021-2022 CopperSpice