Results 1 to 3 of 3

Thread: How to get the color of "checked" QPushButton

  1. #1
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to get the color of "checked" QPushButton

    Hi, all,
    I want to _get_ the color of a standard checked button.

    The purpose is to change the background of an adjacent widget to match the checked color.
    I read sufficiently about palettes and style sheets, and made numerous forum searches with combinations of keywords.
    After getting nowhere I repeated some tests with several brute-force combinations:

    Qt Code:
    1. ui->myButton->setChecked(true); // ensure it is in the correct state
    2.  
    3. // no effect:
    4. // copy myButton color
    5. // overwrite myWidget background color
    6.  
    7. // no effect:
    8. // copy myButton palette
    9. // overwrite myWidget palette
    10.  
    11. // At this point I wasn't sure whether the reading or writing doesn't work.
    12. // Style sheets are always preferred (although this is a very simple requirement)
    13. // so I'll use it based on some examples and other answers.
    14.  
    15. QColor c;
    16. c = ui->myButton->palette().color(QPalette::Active, QPalette::Button);
    17. // or c = ui->myButton->palette().color(QPalette::Active, QPalette::Window);
    18. // or c = ui->myButton->palette().color(QPalette::Active, QPalette::Base);
    19. // or ...any other ColorRole
    20.  
    21. QString s = QString("background-color: rgb(%1,%2,%3)")
    22. .arg(c.red()).arg(c.green()).arg(c.blue());
    23. ui->myWidget->setStyleSheet(s);
    24. ui->myWidget->setAutoFillBackground(true);
    25. ui->myWidget->show();
    To copy to clipboard, switch view to plain text mode 

    Depending on which ColorRole, the widget background changes, so reading the palette color and setting the style sheet work.
    Any ideas how to get the clicked color?
    Scott

  2. #2
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get the color of "checked" QPushButton

    Hi, I made some test - result - pallete colors doesn't changed for checked or unchecked button:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QtGui>
    5.  
    6. class CWidget : public QWidget
    7. {
    8. Q_OBJECT
    9. QPushButton*m_but;
    10. QMap<int,QMap<int,QColor> >m_initial_colors;
    11. QMap<int,QMap<int,QColor> >m_checked_colors;
    12. QMap<int,QMap<int,QColor> >m_unchecked_colors;
    13. public:
    14. CWidget(QWidget*parent=0) : QWidget(parent)
    15. {
    16. m_but = new QPushButton("Button",this);
    17. m_but->setCheckable(true);
    18. setFixedSize(300,300);
    19. m_but->setGeometry(100,100,100,100);
    20. butToggled(false);
    21. connect(m_but,SIGNAL(toggled(bool)),SLOT(butToggled(bool)));
    22. }
    23. private slots:
    24. void storeRoleColor(QMap<int,QMap<int,QColor> >&map,QPalette::ColorGroup group,QPalette::ColorRole role)
    25. {
    26. map[group][role] = m_but->palette().color(group,role);
    27. }
    28.  
    29. void storeGroupColors(QMap<int,QMap<int,QColor> >&map,QPalette::ColorGroup group)
    30. {
    31. storeRoleColor(map,group,QPalette::Window);
    32. storeRoleColor(map,group,QPalette::Background);
    33. storeRoleColor(map,group,QPalette::WindowText);
    34. storeRoleColor(map,group,QPalette::Foreground);
    35. storeRoleColor(map,group,QPalette::Base);
    36. storeRoleColor(map,group,QPalette::AlternateBase);
    37. storeRoleColor(map,group,QPalette::ToolTipBase);
    38. storeRoleColor(map,group,QPalette::ToolTipText);
    39. storeRoleColor(map,group,QPalette::Text);
    40. storeRoleColor(map,group,QPalette::Button);
    41. storeRoleColor(map,group,QPalette::ButtonText);
    42. storeRoleColor(map,group,QPalette::BrightText);
    43. }
    44.  
    45. void storeAllColors(QMap<int,QMap<int,QColor> >&map)
    46. {
    47. storeGroupColors(map,QPalette::Disabled);
    48. storeGroupColors(map,QPalette::Active);
    49. storeGroupColors(map,QPalette::Inactive);
    50. }
    51.  
    52. void butToggled(bool checked)
    53. {
    54. if(m_initial_colors.empty()) {
    55. storeAllColors(m_initial_colors);
    56. } else {
    57. bool print = false;
    58. if(checked) {
    59. storeAllColors(m_checked_colors);
    60. print = ! m_unchecked_colors.empty();
    61. } else {
    62. storeAllColors(m_unchecked_colors);
    63. print = ! m_checked_colors.empty();
    64. }
    65. if(print) {
    66. qDebug() << "result: ";
    67. QMap<int,QMap<int,QColor> >::const_iterator group = m_initial_colors.constBegin();
    68. for(;group != m_initial_colors.constEnd();group++) {
    69. QMap<int,QColor>::const_iterator role = group.value().constBegin();
    70. for(;role != group.value().constEnd();role++) {
    71. int g_id = group.key();
    72. int r_id = role.key();
    73. QColor initial = m_initial_colors[g_id][r_id];
    74. QColor checked = m_checked_colors[g_id][r_id];
    75. QColor unchecked = m_unchecked_colors[g_id][r_id];
    76. QString res;
    77. if( (initial == checked) && (checked == unchecked)) {
    78. res = "SAME";
    79. } else if(checked == unchecked) {
    80. res = "PARTITIALY SAME";
    81. } else {
    82. res = "DIFFERENT";
    83. }
    84.  
    85. qDebug() << "[" << g_id << "," << r_id << "] "
    86. << initial << " : " << checked << " : " << unchecked << " - " << res;
    87.  
    88. }
    89. }
    90. }
    91. }
    92. }
    93. };
    94.  
    95. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    I think that style (with finaly paint standard button) is response that checked button's background is other than unchecked button.

    You can set background color for you button directly (through styleSheet, or through setting pallete after toggling) - this way allows you control button's background at any state.

    P.S. in test above all case response "SAME"

  3. #3
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get the color of "checked" QPushButton

    Thanks, grin, for now I've done a workaround.

    The primary reason for wanting to get the color is to handle other platforms -- no matter where the app is running, the window next to the clicked button looks the same as the button. And also so it doesn't make the user ralph because the override I use doesn't agree with his/her color scheme.

    This doesn't seem like a way-out request, among the hundreds of functions Qt provides for customization.

Similar Threads

  1. Replies: 3
    Last Post: 20th September 2011, 22:37
  2. Replies: 3
    Last Post: 17th March 2010, 16:47
  3. QPushButton - setText with "\n" not working
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 10th February 2008, 03:27
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. border color is wrong with "plastique" style in Qt4.2.3
    By alfa_wu in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2007, 04:27

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.