QFileSystemModel class and QStringListModel class

Introduction to QFileSystemModel

QFileSystemModel is a model class in the Qt framework used to represent the directory structure of the local file system in Qt applications. It provides a convenient way to access and manipulate files and directories in the file system. The following is a detailed introduction to QFileSystemModel:

  1. Representation of directory structure: QFileSystemModel represents the directories and subdirectories of the file system in a tree structure. The root directory is the top-level directory of the file system, and each directory has corresponding subdirectories and files. By using QFileSystemModel, you can easily obtain information such as the name, path, size, and attributes of directories and files.

  2. Use of data model: QFileSystemModel is based on Qt’s data model architecture, so it can be used in conjunction with Qt’s view classes (such as QTreeView) to display the directory structure of the file system. File system browsing and navigation can be achieved by setting QFileSystemModel as the view’s model.

  3. Use of model index: QFileSystemModel uses a model index (QModelIndex) to identify a specific directory or file in the file system. The model index contains information related to specific items, such as the number of rows, columns, parent-child relationships, etc. By using model indexes, files and directories can be accessed, searched, and manipulated easily.

  4. Data update and notification: When the file system changes (such as creating, deleting, renaming files or directories), QFileSystemModel will automatically detect these changes and update the model data in time. After the data is updated, QFileSystemModel will send a signal to notify the relevant view to refresh to reflect the latest status of the file system.

  5. Sorting and filtering: QFileSystemModel supports sorting and filtering directories and files in the file system. You can sort based on attributes such as file name, size, modification date, or hide certain files or directories based on specific filtering rules.

In short, QFileSystemModel provides a convenient way to access and manage the local file system, enabling developers to quickly build powerful file browser, file selector and other applications. By using QFileSystemModel, you can easily handle the directory structure in the file system and implement file and directory related operations.

QFileSystemModel interface

The QFileSystemModel class provides many interfaces (functions and signals) for managing and operating the directory structure of the file system. The following is a detailed introduction to some of the main interfaces:

  1. Constructor:

    • QFileSystemModel(QObject *parent = nullptr): Construct a QFileSystemModel object. The lifecycle of an object can be managed by passing the parent object.
  2. Set and get the root path:

    • setRootPath(const QString & path): Set the root path to the specified path.
    • rootPath() const: Returns the currently set root path.
  3. Get basic information about files and directories:

    • fileName(const QModelIndex & index) const: Returns the name of the file or directory pointed to by the given index.
    • filePath(const QModelIndex & amp;index) const: Returns the full path to the file or directory pointed to by the given index.
    • isDir(const QModelIndex & amp;index) const: Determine whether the given index is a directory.
    • fileInfo(const QModelIndex & amp;index) const: Returns the QFileInfo object of the file or directory pointed to by the given index, including more detailed information, such as file size, creation time, etc.
  4. Get index:

    • index(int row, int column, const QModelIndex & parent = QModelIndex()) const: Returns the model index of the specified row and column under the given parent index.
    • parent(const QModelIndex & amp;child) const: Returns the parent index of the given child index.
  5. Sort and filter:

    • sort(int column, Qt::SortOrder order = Qt::AscendingOrder): Sorts the model’s data according to the specified column and sort order.
    • filter() const: Returns the currently set filter.
    • setFilter(QDir::Filters filters): Set filters to hide certain files or directories.
  6. Signal:

    • directoryLoaded(const QString & path): Emits a signal when the directory loading of the specified path is completed.
    • fileRenamed(const QString & amp;path, const QString & amp;oldName, const QString & amp;newName): Emits a signal when a file is renamed.
    • rootPathChanged(const QString & amp;newPath): Sends a signal when the root path changes.
  7. other:

    • rowCount(const QModelIndex & parent = QModelIndex()) const: Returns the number of child items under a given parent index, or the number of root items if there is no parent index.
    • columnCount(const QModelIndex & amp;parent = QModelIndex()) const: Returns the number of columns for the given parent index.

The above are some commonly used interfaces of the QFileSystemModel class. By using these interfaces, you can easily obtain file and directory information, operate the directory structure of the file system, and combine with the view class to implement powerful file browsing and management applications.

QStringListModel introduction

QStringListModel is a model class in the Qt framework used to represent one-dimensional string lists in Qt applications. It provides a convenient way to manage and operate string data, and can be used with Qt’s view classes (such as QListView, QComboBox, etc.).

The following is a detailed introduction to QStringListModel:

  1. Data storage: QStringListModel uses a list of strings as its data storage. Each item in the list is a string. You can initialize the data of QStringListModel by setting a list of strings, or you can dynamically add, delete, or modify items in the list at runtime.

  2. Use of data model: QStringListModel is based on Qt’s data model architecture, so it can be used in conjunction with Qt’s view classes (such as QListView, QComboBox). By setting QStringListModel as the model of the view, functions such as string display, selection, and editing can be achieved.

  3. Data update and notification: When the string list changes, such as adding, deleting, or modifying items in the list, QStringListModel will automatically detect these changes and update the model data in time. After the data is updated, QStringListModel sends a signal to notify the relevant view to refresh to reflect the latest string list.

  4. Data access: Through QStringListModel, you can easily access string data in the list. You can use the index() function to get the model index of the specified row and column, and use the data() function to get the string data at the index position.

  5. Data manipulation: QStringListModel provides functions to easily manipulate string lists, such as adding, deleting, inserting, and modifying items in the list. The list can be modified using functions such as insertRows(), removeRows(), and setData().

In summary, QStringListModel is a convenient and flexible model class for managing and manipulating string list data in Qt applications. By using QStringListModel, you can easily display, edit and select string data, and combine it with the corresponding view class to achieve the required functions.

QStringListModel interface

The QStringListModel class provides many interfaces (functions and signals) for managing and manipulating string list data. The following is a detailed introduction to some of the main interfaces:

  1. Constructor:

    • QStringListModel(QObject *parent = nullptr): Construct a QStringListModel object. The lifecycle of an object can be managed by passing the parent object.
  2. Data operations:

    • setStringList(const QStringList & strings): Set the model’s string list to the specified strings.
    • stringList() const: Returns the currently set string list.
    • insertRows(int row, int count, const QModelIndex & parent = QModelIndex()): Insert count blank rows at the specified row.
    • removeRows(int row, int count, const QModelIndex & parent = QModelIndex()): Delete count rows starting from the specified row.
    • setData(const QModelIndex & amp;index, const QVariant & amp;value, int role = Qt::EditRole): Set the data at the specified index position as value, and use role to identify the role of the data.
  3. data access:

    • index(int row, int column = 0, const QModelIndex & amp;parent = QModelIndex()) const: Returns the model index of the specified row and column under the given parent index.
    • data(const QModelIndex & amp;index, int role = Qt::DisplayRole) const: returns the data at the given index, you can use role to specify the role of the data to be obtained.
    • flags(const QModelIndex & amp;index) const: Returns the flag of the data at the given index, used to indicate the properties of the data, such as whether it can be edited, whether it can be selected, etc.
    • rowCount(const QModelIndex & parent = QModelIndex()) const: Returns the number of children under the given parent index, or the number of rows in the model if there is no parent index.
    • columnCount(const QModelIndex & amp;parent = QModelIndex()) const: Returns the number of columns for the given parent index, the default is 1 column.
  4. Signal:

    • dataChanged(const QModelIndex & amp;topLeft, const QModelIndex & amp;bottomRight, const QVector & amp;roles = QVector()): When the data changes, a signal is sent to notify the relevant view to refresh the interface.
  5. other:

    • insertRow(int row, const QModelIndex & parent = QModelIndex()): Insert a row at the specified row.
    • removeRow(int row, const QModelIndex & parent = QModelIndex()): Delete the specified row.
    • headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const: Returns the header data of the specified section (row or column), which can be used for header display.
    • setDataList(const QStringList & strings): Sets the model’s string list to the specified strings (same function as setStringList()).
    • dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex & amp;parent): Accepts data dragged and dropped from other applications.

The above are some commonly used interfaces of the QStringListModel class. By using these interfaces, you can easily operate and manage the data of the string list, and realize various functions in combination with the view class.

Code demonstration

#include <QApplication>
#include <QMainWindow>
#include <QFileSystemModel>
#include <QTreeView>
#include <QListView>
#include <QTableView>
#include <QModelIndex>
#include <QDir>
#include <QLabel>
#include <QCheckBox>
#include <QVBoxLayout>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow mainWindow;
    QFileSystemModel model( & amp;mainWindow); // Create QFileSystemModel with parent as mainWindow
    model.setRootPath(QDir::currentPath()); // Set root path to current directory

    QTreeView treeView( & amp;mainWindow); // Create QTreeView
    treeView.setModel( & amp;model); // Set QFileSystemModel as data model for treeView

    QListView listView( & amp;mainWindow); // Create QListView
    listView.setModel( & amp;model); // Set QFileSystemModel as data model for listView

    QTableView tableView( & amp;mainWindow); // Create QTableView
    tableView.setModel( & amp;model); // Set QFileSystemModel as data model for tableView

    QObject::connect( & amp;treeView, & amp;QTreeView::clicked, & amp;listView, & amp;QListView::setRootIndex);
    QObject::connect( & amp;treeView, & amp;QTreeView::clicked, & amp;tableView, & amp;QTableView::setRootIndex);

    QWidget *centralWidget = new QWidget( & amp;mainWindow); // Create central widget
    QVBoxLayout *layout = new QVBoxLayout(centralWidget); // Create layout for central widget
    layout->addWidget( & amp;treeView); // Add treeView to layout
    layout->addWidget( & amp;listView); // Add listView to layout
    layout->addWidget( & amp;tableView); // Add tableView to layout

    QLabel labelPath( & amp;mainWindow); // Create QLabel to display selected item's path
    QLabel labelType( & amp;mainWindow); // Create QLabel to display selected item's type
    QLabel labelFileName( & amp;mainWindow); // Create QLabel to display selected item's file name
    QLabel labelFileSize( & amp;mainWindow); // Create QLabel to display selected item's file size
    QCheckBox checkBoxIsDir( & amp;mainWindow); // Create QCheckBox to display if selected item is a directory

    QObject::connect( & amp;treeView, & amp;QTreeView::clicked, [ & amp;]() {
        QModelIndex index = treeView.currentIndex();
        checkBoxIsDir.setChecked(model.isDir(index));
        labelPath.setText(model.filePath(index));
        labelType.setText(model.type(index));

        QString fileName = model.fileName(index);
        labelFileName.setText(fileName);

        qint64 fileSize = model.size(index) / 1024;
        if (fileSize < 1024)
            labelFileSize.setText(QString("%1 KB").arg(fileSize));
        else
            labelFileSize.setText(QString::asprintf("%.1f MB", fileSize / 1024.0));
    });

    layout->addWidget( & amp;checkBoxIsDir); // Add checkBoxIsDir to layout
    layout->addWidget( & amp;labelPath); // Add labelPath to layout
    layout->addWidget( & amp;labelType); // Add labelType to layout
    layout->addWidget( & amp;labelFileName); // Add labelFileName to layout
    layout->addWidget( & amp;labelFileSize); // Add labelFileSize to layout

    mainWindow.setCentralWidget(centralWidget); // Set central widget for mainWindow
    mainWindow.resize(1000,1000);
    mainWindow.show();


    return app.exec();
}

running result

Code demo 2:

#include <QApplication>
#include <QListView>
#include <QStringListModel>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QStringListModel model; // Create QStringListModel object

    //Set string list data
    QStringList dataList;
    dataList << "Apple" << "Banana" << "Grapes" << "Orange";
    model.setStringList(dataList);

    QListView listView; // Create QListView object
    listView.setModel( & amp;model); // Set QStringListModel as the data model of QListView

    // Add, delete and insert items in QStringListModel
    model.insertRows(2, 1); // Insert 1 item at index 2
    model.setData(model.index(2), "Cherry"); // Set the data of the item at index 2
    model.removeRows(1, 1); // Remove 1 item at index 1

    // Get the data in QStringListModel
    QStringList result = model.stringList();
    qDebug() << "Data in QStringListModel:";
    for (const QString & amp; item : result) {
        qDebug() << item;
    }

    listView.show();

    return app.exec();
}

running result

The output is as follows:

Data in QStringListModel:

“Apple”

“Cherry”

“Grapes”

“Orange”

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge MySQL entry skill tree HomepageOverview 66001 people are studying systematically