0

how to make selected items turn into green?

QVariant DomModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    DomItem *item = static_cast<DomItem*>(index.internalPointer());
    const QDomNode node = item->node();

    if ( role == Qt::CheckStateRole && (index.column() == 0) && hasChildren(index) )
    {
        return static_cast< int >( item->isChecked() ? Qt::Checked : Qt::Unchecked );
}
    if (role == Qt::FontRole && item->isChecked()) {
            QFont font;
            font.setBold(true);
            return font;
    }
    
    if (role != Qt::DisplayRole)
        return QVariant();

    switch (index.column()) {
        case 0:
            return node.nodeName();
        case 1:
        {
            const QDomNamedNodeMap attributeMap = node.attributes();
            QStringList attributes;
            for (int i = 0; i < attributeMap.count(); ++i) {
                QDomNode attribute = attributeMap.item(i);
                attributes << attribute.nodeName() + "=\""
                              + attribute.nodeValue() + '"';
            }
            return attributes.join(' ');
        }
        case 2:
            return node.nodeValue().split('\n').join(' ');
        default:
            break;

    }

    return item->data(index.column());
}

In the role I have done Items which is selected to be Bold when checked, But how change that fonts color to green which is checked?

Please Look at this Image:

enter image description here

Arnab
  • 4,216
  • 2
  • 28
  • 50
  • You could use stylesheets. e.g: checkBox->setStyleSheet("QCheckBox { color: red }"); – BIg G Dec 07 '22 at 09:14
  • This question was already solved https://stackoverflow.com/questions/11153945/how-to-change-qcheckbox-text-label-color-in-qt – BIg G Dec 07 '22 at 09:15
  • It didn't worked –  Dec 07 '22 at 10:05
  • Looks like a duplicate: https://stackoverflow.com/questions/57954368/qabstracttablemodel-updating-background-color – Arman Oganesyan Dec 07 '22 at 10:14
  • Does this answer your question? [QAbstractTableModel updating background color](https://stackoverflow.com/questions/57954368/qabstracttablemodel-updating-background-color) – Greenonline Dec 08 '22 at 14:20

1 Answers1

1

Take a look at role BackgroundRole:

if (role == Qt::BackgroundRole)
{
    return QColor(Qt::lightGray);
}
Arman Oganesyan
  • 396
  • 3
  • 11