2016-01-08 23 views
0

我有一個QListView的模型,如果我執行model->data(idx);它返回以下QvariantQVariant(QString, "Colorful text")。我的任務是編輯Colorful text的顏色。如何更改QListView中的QString項目的顏色

我想使用水印像model->setData(idx, *dunno what put here*, Qt::EditRole); 我希望我需要把顏色描述,以*dunno what put here*的地方,但我不知道如何。

你能幫我嗎?

回答

0

爲了從model->data(idx)提取文本顏色,你必須使用角色Qt::ForegroundRole作爲附加parameter返回一個QVariant v

然後你就可以檢查你的QVariant對象的值,例如與

int c = v.value<int>(); 

最後模型通過

v.setValue(c); 
model->setData(idx, const QVariant & value, Qt::ForegroundRole); 
+0

使用setData(設置你的新的,修改顏色回一個整數)只能當模型實現它 - 如果模型在您的控制範圍內,則使用data()來返回ForegroundRole所需的顏色 –

相關問題