我想为我的列表视图模型中的选定行设置背景颜色。选择另一行后,上一行的颜色变得透明。谢谢!Qt,QListView模型
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
if (role == Qt::DisplayRole) {
qDebug() << "get row:" << index.row();
//auto sp = pets[index.row()].getSpecies();
//return QString::fromStdString(sp);
string tara = v[index.row()].getTara();
int pct = v[index.row()].getPct();
QString linie;
linie.append(QString::fromStdString(tara));
linie.append(" ");
linie.append(QString::number(pct));
return linie;
}
if (role == Qt::BackgroundColorRole)
{
QBrush redBackground(Qt::red);//here ,i don't now to put a condition when row is selected
return redBackground;
}
return QVariant{};
}
//here i try to brush the selected row
QObject::connect(lst->selectionModel(), &QItemSelectionModel::selectionChanged, this, &Console::onSelectionChanged);
void Console::onSelectionChanged() {
auto sel = lst->selectionModel()->selectedIndexes();
QModelIndex firstSel = sel.at(0);
Mymodel->setData(firstSel, QBrush(Qt::yellow), Qt::BackgroundColorRole);
//Console is a class which inherits QWidget,here is a QListView* lst
可能,您需要查看代表:http://doc.qt.io/qt-5/qitemdelegate.html#paint。没有确切的问题和一些SSCCE /屏幕截图很难说。 –
仍然看不到最终的'data'和'setData'实现。 –
我没有任何数据和setData的实现 – paulc