QAbstractItemModel模拟文件管理器的完整代码
时间: 2024-01-15 15:05:21 浏览: 111
以下是使用QAbstractItemModel模拟文件管理器的完整代码示例:
```cpp
#include <QtWidgets>
class FileModel : public QAbstractItemModel
{
public:
FileModel(QObject *parent = nullptr);
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
QStringList headers;
QMap<QString, QStringList> files;
};
FileModel::FileModel(QObject *parent)
: QAbstractItemModel(parent)
{
headers << "Name" << "Size" << "Type";
files.insert("Documents", QStringList() << "Report.docx" << "Presentation.pptx" << "Spreadsheet.xlsx");
files.insert("Pictures", QStringList() << "Nature.jpg" << "Cityscape.jpg" << "Portrait.jpg");
files.insert("Music", QStringList() << "Rock.mp3" << "Pop.mp3" << "Classical.mp3");
}
QModelIndex FileModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!parent.isValid())
return createIndex(row, column, headers[row]);
QString parentName = parent.internalPointer() ? static_cast<QString>(parent.internalPointer()) : "";
return createIndex(row, column, files[parentName][row]);
}
QModelIndex FileModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
QString fileName = static_cast<QString>(index.internalPointer());
for (const auto &folder : files.keys()) {
if (files[folder].contains(fileName))
return createIndex(headers.indexOf(folder), 0, folder);
}
return QModelIndex();
}
int FileModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return headers.size();
QString parentName = parent.internalPointer() ? static_cast<QString>(parent.internalPointer()) : "";
return files[parentName].size();
}
int FileModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return headers.size();
}
QVariant FileModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
if (!index.parent().isValid()) {
return headers[index.row()];
} else {
QString parentName = index.parent().internalPointer() ? static_cast<QString>(index.parent().internalPointer()) : "";
return files[parentName][index.row()];
}
}
return QVariant();
}
bool FileModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
if (!index.isValid())
return false;
QString fileName = static_cast<QString>(index.internalPointer());
QString parentName = index.parent().internalPointer() ? static_cast<QString>(index.parent().internalPointer()) : "";
int row = files[parentName].indexOf(fileName);
if (row == -1)
return false;
files[parentName][row] = value.toString();
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
Qt::ItemFlags FileModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView treeView;
FileModel fileModel;
treeView.setModel(&fileModel);
treeView.show();
return a.exec();
}
```
在上面的代码中,我们定义了一个`FileModel`类,该类继承自`QAbstractItemModel`。该模型用于模拟文件管理器,并使用`QTreeView`进行显示。
模型中的数据由`QMap<QString, QStringList>`来存储,其中键为文件夹名称,值为该文件夹中的文件列表。在模型中实现了必需的虚拟函数,以便可以将模型提供给`QTreeView`使用。
在`main()`函数中,我们创建了一个`QTreeView`并将其关联到`FileModel`。最后,我们调用应用程序的执行函数`a.exec()`以启动应用程序并显示窗口和树视图。
阅读全文