首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt,QListView模型

Qt,QListView模型
EN

Stack Overflow用户
提问于 2016-06-15 05:14:41
回答 1查看 1.5K关注 0票数 0

我想从我的列表视图中为所选行设置背景颜色model.After选择另一行,前一行的颜色是make transparent.Thanks!

代码语言:javascript
复制
    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
EN

回答 1

Stack Overflow用户

发布于 2016-06-15 06:56:08

你需要通过视图的selection model来跟踪选择。修改选择后,您可以将数据设置到模型中。例如:model->setData( selectedIndex, QBrush(Qt::red), Qt::BackgroundColorRole );

您应该明白,一个模型可以分配给多个视图。为了更深入地理解,我建议你阅读关于model-view programming in qt的文章。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37822216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档