Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Combo boxes - dynamic font color change

Combo boxes - dynamic font color change

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rastaqki
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      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.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved