Qt:QFileSystemModel 使用記錄

QFileSystemModel類:Qt幫助中的介紹爲:

The QFileSystemModel class provides a data model for the local filesystem.
This class provides access to the local filesystem, providing functions for renaming and removing files and directories, and for creating new directories. In the simplest case, it can be used with a suitable display widget as part of a browser or filter.
QFileSystemModel can be accessed using the standard interface provided by QAbstractItemModel, but it also provides some convenience functions that are specific to a directory model. The fileInfo(), isDir(), fileName() and filePath() functions provide information about the underlying files and directories related to items in the model. Directories can be created and removed using mkdir(), rmdir().
Note: QFileSystemModel requires an instance of QApplication.

大意爲:

QFileSystemModel類爲本地文件系統提供了一個數據模型。

這個類提供對本地文件系統的訪問,提供重命名和刪除文件和目錄以及創建新目錄的功能。在最簡單的情況下,它可以與適當的顯示小部件一起作爲瀏覽器或過濾器的一部分使用。

可以使用QAbstractItemModel提供的標準接口訪問QFileSystemModel,但是它也提供了一些特定於目錄模型的方便函數。fileInfo()、isDir()、fileName()和filePath()函數提供關於與模型中的項相關的底層文件和目錄的信息。可以使用mkdir()、rmdir()創建和刪除目錄。

注意:QFileSystemModel需要QApplication的一個實例

調用該的類的方式爲:

    model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());
    ui->tableView->setModel(model);

上面的setRootPath 並不會讓 tableVew 顯示 QDir::currentPath() 目錄。想要顯示當前目錄,可以使用:

ui->tableView->setRootIndex(model->index(QDir::currentPath()));


上面顯示的表格窗口一共有四行,分別爲:Name 、Size、Type還有“Date Modified”。表頭定義是在Qt源碼的:qtbase/src/widgets/dialogs/qfilesystemmodel.cpp 文件中QFileSystemModel::headerData函數中實現。

所以,想要修改表頭內容,可以重寫該函數。下面是具體例子。
mainwindow.h 文件內容爲:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileSystemModel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MyFileSystemModel : public QFileSystemModel
{
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;

};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    MyFileSystemModel *model;

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new MyFileSystemModel;
    model->setRootPath(QDir::currentPath());
    ui->tableView->setModel(model);

    ui->tableView->setRootIndex(model->index(QDir::currentPath()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

/*!
    \reimp
*/
QVariant MyFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    switch (role) {
    case Qt::DecorationRole:
        if (section == 0) {
            // ### TODO oh man this is ugly and doesn't even work all the way!
            // it is still 2 pixels off
            QImage pixmap(16, 1, QImage::Format_Mono);
            pixmap.fill(0);
            pixmap.setAlphaChannel(pixmap.createAlphaMask());
            return pixmap;
        }
        break;
    case Qt::TextAlignmentRole:
        return Qt::AlignLeft;
    }

    if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
        return QAbstractItemModel::headerData(section, orientation, role);

    QString returnValue;
    switch (section) {
    case 0: returnValue = tr("Name");
            break;
    case 1: returnValue = tr("Size");
            break;
    case 2: returnValue =
#ifdef Q_OS_MAC
                   tr("Kind", "Match OS X Finder");
#else
                   tr("Type", "All other platforms");
#endif
           break;
    // Windows   - Type
    // OS X      - Kind
    // Konqueror - File Type
    // Nautilus  - Type
    case 3: returnValue = tr("adams_path");
            break;
    default: return QVariant();
    }
    return returnValue;
}

這樣就實現了修改表頭的功能。

但是:

上面僅僅實現了修改表頭內容,下面的數據,依然是修改日期。

想要修改下面的數據,需要重寫QFileSystemModel::data函數,但是該函數中調用了私有成員,不能直接修改重寫。

所以還是需要,一行一行的手動修改數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章