QAbstractItemModel实现setRootPath的C++完整代码
时间: 2024-05-01 11:23:15 浏览: 186
CustomTreeViewDemo.rar
以下是使用QAbstractItemModel实现setRootPath的C++完整代码示例:
```
#include <QAbstractItemModel>
#include <QFileInfo>
#include <QList>
#include <QVariant>
class FileSystemModel : public QAbstractItemModel {
public:
explicit FileSystemModel(QObject* parent = nullptr) : QAbstractItemModel(parent), rootItem(nullptr) {
QList<QVariant> rootData;
rootData << "Name" << "Size" << "Type";
rootItem = new FileSystemModelItem(rootData);
rootItem->setFilePath("");
setupModelData(rootItem);
}
~FileSystemModel() {
delete rootItem;
}
QVariant data(const QModelIndex& index, int role) const override {
if (!index.isValid()) {
return QVariant();
}
if (role != Qt::DisplayRole) {
return QVariant();
}
FileSystemModelItem* item = static_cast<FileSystemModelItem*>(index.internalPointer());
return item->data(index.column());
}
Qt::ItemFlags flags(const QModelIndex& index) const override {
if (!index.isValid()) {
return nullptr;
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return rootItem->data(section);
}
return QVariant();
}
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override {
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
FileSystemModelItem* parentItem;
if (!parent.isValid()) {
parentItem = rootItem;
} else {
parentItem = static_cast<FileSystemModelItem*>(parent.internalPointer());
}
FileSystemModelItem* childItem = parentItem->child(row);
if (childItem) {
return createIndex(row, column, childItem);
}
return QModelIndex();
}
QModelIndex parent(const QModelIndex& index) const override {
if (!index.isValid()) {
return QModelIndex();
}
FileSystemModelItem* childItem = static_cast<FileSystemModelItem*>(index.internalPointer());
FileSystemModelItem* parentItem = childItem->parent();
if (parentItem == rootItem) {
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
int rowCount(const QModelIndex& parent = QModelIndex()) const override {
FileSystemModelItem* parentItem;
if (parent.column() > 0) {
return 0;
}
if (!parent.isValid()) {
parentItem = rootItem;
} else {
parentItem = static_cast<FileSystemModelItem*>(parent.internalPointer());
}
return parentItem->childCount();
}
int columnCount(const QModelIndex& parent = QModelIndex()) const override {
if (parent.isValid()) {
return static_cast<FileSystemModelItem*>(parent.internalPointer())->columnCount();
}
return rootItem->columnCount();
}
void setRootPath(const QString& path) {
rootItem->setFilePath(path);
setupModelData(rootItem);
}
private:
void setupModelData(FileSystemModelItem* parent) {
QString filePath = parent->filePath();
QFileInfo fileInfo(filePath);
QDir dir(filePath);
QList<QVariant> data;
data << fileInfo.fileName();
if (fileInfo.isFile()) {
data << fileInfo.size();
data << fileInfo.suffix();
parent->setData(data);
return;
}
data << "";
data << "Folder";
parent->setData(data);
QList<QVariant> row;
QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
for (const QFileInfo& fileInfo : fileList) {
row.clear();
row << fileInfo.fileName() << fileInfo.size() << fileInfo.suffix();
FileSystemModelItem* item = new FileSystemModelItem(row, parent);
item->setFilePath(fileInfo.filePath());
parent->appendChild(item);
}
QFileInfoList folderList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
for (const QFileInfo& folderInfo : folderList) {
row.clear();
row << folderInfo.fileName() << "" << "Folder";
FileSystemModelItem* item = new FileSystemModelItem(row, parent);
item->setFilePath(folderInfo.filePath());
parent->appendChild(item);
setupModelData(item);
}
}
FileSystemModelItem* rootItem;
};
class FileSystemModelItem {
public:
explicit FileSystemModelItem(const QList<QVariant>& data, FileSystemModelItem* parent = nullptr) : parentItem(parent), itemData(data) {
}
~FileSystemModelItem() {
qDeleteAll(childItems);
}
void appendChild(FileSystemModelItem* child) {
childItems.append(child);
}
FileSystemModelItem* child(int row) {
return childItems.value(row);
}
int childCount() const {
return childItems.count();
}
int columnCount() const {
return itemData.count();
}
QVariant data(int column) const {
return itemData.value(column);
}
FileSystemModelItem* parent() {
return parentItem;
}
int row() const {
if (parentItem) {
return parentItem->childItems.indexOf(const_cast<FileSystemModelItem*>(this));
}
return 0;
}
void setData(const QList<QVariant>& data) {
itemData = data;
}
QString filePath() const {
return m_filePath;
}
void setFilePath(const QString& filePath) {
m_filePath = filePath;
}
private:
QList<FileSystemModelItem*> childItems;
QList<QVariant> itemData;
FileSystemModelItem* parentItem;
QString m_filePath;
};
```
在此示例中,我们定义了一个FileSystemModel类,它继承自QAbstractItemModel,并实现了setRootPath方法。setRootPath方法接收一个路径作为参数,并使用该路径更新根项的文件路径,然后调用setupModelData方法重新设置模型数据。setupModelData方法解析文件夹并将其添加到模型中作为子项。注意,列数据是使用QList<QVariant>传递的,这样我们可以在列中使用不同的数据类型。此外,我们还定义了一个FileSystemModelItem类,它表示模型中的一个项目。每个项目都有一个文件路径,并且可以有多个子项。
阅读全文