Home > Blockchain >  How to make selected checkbox Item color to Green in QT?
How to make selected checkbox Item color to Green in QT?

Time:12-07

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

CodePudding user response:

Take a look at role BackgroundRole:

if (role == Qt::BackgroundRole)
{
    return QColor(Qt::lightGray);
}
  • Related