簡體   English   中英

QT - QTreeView具有不同顏色的QTreeView項目的子組

[英]QT - QTreeView with different colors for subgroups of the QTreeView items

有誰知道如何實現/實現QTreeView用不同顏色的子群QTreeView項目?
就像是:

1

有沒有人做過這樣的事情,可以給我一個教程的鏈接或如何,或示例代碼也會很好。 目前我完全不知道如何建立這個。

我使用Qt 5.1.1的工作,並使用QTreeViewQFileSystemModelQItemSelectionModel

我還想到了:
m_TreeView->setStyleSheet(...)
但這僅為整個treeView設置樣式,或僅為所選treeView樣式設置樣式。

有什么建議么? 非常感謝你的幫助!

Qt :: BackgroundRole可用於返回視圖用來繪制索引背景的QColor。

當您使用現有項目模型類(QFileSystemModel)時,最簡單的方法是將代理模型置於文件系統模型之上,只進行着色。

使用QIdentityProxyModel

class ColorizeProxyModel : public QIdentityProxyModel {
    Q_OBJECT
public:
    explicit ColorizeProxyModel(QObject *parent = 0) : QIdentityProxyModel(parent) {}

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
        if (role != Qt::BackgroundRole) 
            return QIdentityProxyModel::data(index, role);

        ... find out color for index 
        return color;
    }
};

要使用它:

QFileSystemModel *fsModel = new QFileSystemModel(this);
ColorizeProxyModel *colorProxy = new ColorizeProxyModel(this);
colorProxy->setSourceModel(fsModel);
treeView->setModel(colorProxy);

如果您需要更多花哨的東西(如特殊形狀等),您需要自己的項目委托與自定義繪畫(請參閱QStyledItemDelegate )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM