Combo boxes - dynamic font color change



  • Hello,
    I'm new to Qt, but I know C++ pretty well. I was searching about my problem and know that there is a couple ways of doing that (custom model, custom delegate, stylesheets).

    Details about my goal:

    • several combo boxes (up to 4) with the same items (serial ports names)
    • no filtering (any port can be set on any combo box)
    • only indication that we chose the same ports in 2 or more boxes
    • indication as red, bolded ports names in combo boxes (both selected and on expanded list)
    • slot that checks current indexes and sets desired formatting

    I need the simplest way of doing that. I'm trying but as I said I'm new and got mixed up a lot now. Can't manage to write working code using methods mentioned above. I really don't care if it's single items or model/view I just need to achieve my goal.

    P.S. Sorry for my english, it's not my native language.



  • You could use a delegate to control this but it can be even easier if you have a custom combo box you can just set the foreground role to the color/boldness you want. Here is a quick color example:

    @
    #include <QComboBox>
    #include <QApplication>
    #include <QBrush>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

        QComboBox box;
        box.addItems(QStringList() << "Red Text" << "Blue Text" << "Green Text");
        box.setItemData(0, QBrush(QColor(Qt::red)), Qt::ForegroundRole);
        box.setItemData(1, QBrush(QColor(Qt::blue)), Qt::ForegroundRole);
        box.setItemData(2, QBrush(QColor(Qt::green)), Qt::ForegroundRole);
        box.show();
    
        return app.exec();
    

    }
    @

    You can set all kinds of roles to whatever you want, check the Qt docs for role information.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.