Qt World Summit: Submit your Presentation

QListView add item text customization

  • Hi,
    does anybody know how to set text color for new added item into data model?
    My code looks like:

        myModel->insertRows(0, 1);
        QModelIndex index = myModel->index(0);
        this->m_logModel->setData(index, log); //this works fine !!!
        QBrush yellow;
        this->m_logModel->setData(index, yellow, Qt::TextColorRole); ///this not !!! text color is still black...

    Best regards,

  • What is m_logModel, what is myModel?

    Your data model may or may not implement some roles.
    The QAbstractItemModel class implementation does nothing and returns false.
    It is a good idea to check what setData returns.

  • Moderators

    First of all, as mentioned in the docs, Qt::TextColorRole is obsolete and you should not use it. Use Qt::ForegroundRole instead.
    Second, as mentioned by @alex_malyu, it depends on the model whether or not it implements that. QStandardItemModel for example will, but your custom model might not.
    So the question is what is your model class.

Log in to reply