Results 1 to 6 of 6

Thread: change color of a QComboBox line

  1. #1
    Join Date
    Jan 2025
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default change color of a QComboBox line

    How to change the color of a row in a QComboBox?

    Qt Code:
    1. #include "modifcons.h"
    2.  
    3. ModifCons::ModifCons()
    4. {
    5. qDebug() << "modifcons";
    6.  
    7. m_modifConsignes = new Consignes(QString("settings/consignes.ini"), QSettings::IniFormat);
    8.  
    9. m_window2 = new QWidget;
    10. m_window2 ->setFixedSize(1000,600);
    11. m_window2 ->setWindowTitle("Modification Consignes Gainable");
    12. adjustSize();
    13.  
    14. m_label2 = new QLabel(m_window2);
    15. m_label2 ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/modifCons.jpg"));
    16. m_label2 ->showMaximized();
    17.  
    18. select = new QComboBox(m_window2);
    19. select ->setGeometry(50,50,900,120);
    20. for (int i(0); i < 16; ++i)
    21. {
    22. select ->addItem(QString::number(i));
    23. }
    24. select ->setStyleSheet("font-size: 30px;");
    25. select ->setItemText(0,"Consigne Ete/Hiver :");
    26. select ->setItemText(1,"Consigne Canicule :");
    27. select ->setItemText(2,"Consigne Bloquage Chauffage :");
    28. select ->setItemText(3,"Consigne Mode Degivrage (Naturel ou Electrique) Chauffage :");
    29. select ->setItemText(4,"Consigne Grande Vitesse Ventilation Exterieur Chauffage :");
    30. select ->setItemText(5,"Consigne Grande Vitesse Ventilation Exterieur Froid :");
    31. select ->setItemText(6,"Consigne Fin Degivrage Chauffage :");
    32. select ->setItemText(7,"Consigne Depart Chauffage en Mode Chauffage :");
    33. select ->setItemText(8,"Consigne Depart Froid en Mode Chauffage :");
    34. select ->setItemText(9,"Consigne Petite Vitesse Ventilation Interieur Chauffage :");
    35. select ->setItemText(10,"Consigne Depart Froid en Mode Froid :");
    36. select ->setItemText(11,"Consigne Depart Chauffage en Mode Froid :");
    37. select ->setItemText(12,"Consigne Petite Vitesse Ventilation Interieur Froid :");
    38. select ->setItemText(13,"Consigne Depart Ventilation Interieur Chauffage :");
    39. select ->setItemText(14,"Consigne Lancement Degivrage Froid :");
    40. select ->setItemText(15,"Consigne Fin Degivrage Froid :");
    41.  
    42. m_valid1 = new QPushButton("valid",m_window2);
    43. m_valid1 ->setGeometry(50,180,240,120);
    44. m_valid1 ->setStyleSheet("font-size: 30px;");
    45.  
    46. m_quit1 = new QPushButton("quitter",m_window2);
    47. m_quit1 ->setGeometry(710,440,240,120);
    48. m_quit1 ->setStyleSheet("font-size: 30px;background-color: red");
    49.  
    50. connect(m_valid1, &QPushButton::clicked, this, &ModifCons::selectCons);
    51. connect(m_quit1,&QPushButton::clicked,m_quit1,[this](){m_window2 ->close(); delete m_window2;});
    52.  
    53. m_window2 ->show();
    54. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change color of a QComboBox line

    You can try this:

    Qt Code:
    1. select->setItemData( index, color, Qt::ForegroundRole );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2025
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: change color of a QComboBox line

    Quote Originally Posted by d_stranz View Post
    You can try this:

    Qt Code:
    1. select->setItemData( index, color, Qt::ForegroundRole );
    To copy to clipboard, switch view to plain text mode 
    does not work

    I want to change the color of:
    Qt Code:
    1. select ->setItemText(1,"Consigne Canicule :");
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change color of a QComboBox line

    does not work
    Sorry, I guess that is only used for setting custom data and not visual properties.

    I think the only way to change the color of an individual entry in a combo box is the implement a custom QAbstractItemModel for the items you want to add, and use your model's QAbstractItemModel::data() method to return the colors you want using the Qt::ForegroundRole role. You should study the Qt Model / View architecture to learn about this.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2025
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: change color of a QComboBox line

    Quote Originally Posted by d_stranz View Post
    Sorry, I guess that is only used for setting custom data and not visual properties.

    I think the only way to change the color of an individual entry in a combo box is the implement a custom QAbstractItemModel for the items you want to add, and use your model's QAbstractItemModel::data() method to return the colors you want using the Qt::ForegroundRole role. You should study the Qt Model / View architecture to learn about this.
    when I do this it works:
    Qt Code:
    1. select ->setItemData(1, QColor(255, 175, 0), Qt::TextColorRole);
    To copy to clipboard, switch view to plain text mode 

    but I get a warning:

    [ 11%] Building CXX object CMakeFiles/test.dir/modifcons.cpp.o
    /home/ludo/Qt/test2/modifcons.cpp: In constructor 'ModifCons::ModifCons()':
    /home/ludo/Qt/test2/modifcons.cpp:27:54: warning: 'Qt::TextColorRole' is deprecated [-Wdeprecated-declarations]
    27 | select ->setItemData(1, QColor(255, 175, 0), Qt::TextColorRole);
    | ^~~~~~~~~~~~~
    In file included from /usr/include/aarch64-linux-gnu/qt5/QtCore/qobjectdefs.h:48,
    from /usr/include/aarch64-linux-gnu/qt5/QtCore/qobject.h:46,
    from /usr/include/aarch64-linux-gnu/qt5/QtCore/qabstractanimation.h:43,
    from /usr/include/aarch64-linux-gnu/qt5/QtCore/QtCore:6,
    from /usr/include/aarch64-linux-gnu/qt5/QtWidgets/QtWidgetsDepends:3,
    from /usr/include/aarch64-linux-gnu/qt5/QtWidgets/QtWidgets:3,
    from /home/ludo/Qt/test2/modifcons.h:4,
    from /home/ludo/Qt/test2/modifcons.cpp:1:
    /usr/include/aarch64-linux-gnu/qt5/QtCore/qnamespace.h:1547:9: note: declared here
    1547 | TextColorRole Q_DECL_ENUMERATOR_DEPRECATED = ForegroundRole,

  6. #6
    Join Date
    Jan 2025
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: change color of a QComboBox line

    It's good, I found it
    select ->setItemData(2, QColor(255, 0, 0), Qt::ForegroundRole);
    so solved thanks

Similar Threads

  1. Replies: 0
    Last Post: 2nd December 2020, 16:06
  2. Replies: 4
    Last Post: 19th July 2016, 19:10
  3. Replies: 2
    Last Post: 9th March 2015, 11:02
  4. how to change the QComboBox focus color?
    By josentop in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2011, 16:20
  5. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.