gpt4 book ai didi

c++ - 当 QFilesystemModel 显示驱动器时,可以在 QTreeView 中更改文件夹颜色吗?

转载 作者:行者123 更新时间:2023-11-28 07:50:49 27 4
gpt4 key购买 nike

我一直在开发一个 Qt 应用程序,我需要使用 QFilesystemModel 显示文件系统。我已经能够按预期显示它。

代码:

QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath())
tree->setModel(model);

这会显示 QTreeView 中的所有驱动器。但我们都知道,默认情况下,每个驱动器中的文件夹颜色是黄色

这就是我想要改变的。 Qt 中有没有一种方法可以将文件夹的颜色更改为“蓝色”???

最佳答案

您可以通过 qt 委托(delegate)自定义您的 QTreeView。但如果它只是一个图标,我认为你可以重新实现:

class MyQFileSystemModel : public QFileSystemModel {
public:
QVariant data( const QModelIndex& index, int role ) const {

if( role == Qt::DecorationRole ) {
return QVariant(QIcon(QPixmap("icon.png")));
}

return QFileSystemModel::data(index, role);
}
};

要了解委托(delegate),我建议您研究示例:Spin box delegate

编辑:您必须从 QFileSystemModel 重新实现方法数据,您必须从 QFileSystemModel 继承。

然后你像以前一样做:

MyQFileSystemModel* model = new MyQFileSystemModel;
model->setRootPath(QDir::currentPath())
tree->setModel(model);

关于c++ - 当 QFilesystemModel 显示驱动器时,可以在 QTreeView 中更改文件夹颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13817010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com