title: How to get the background color of QT-toolButton/pushButton using C++
body: <p><strong>Related Qusetions:</strong></p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/15519749/how-to-get-widget-background-qcolor">How to get widget background QColor</a> <code>QWidget::palette().color(QWidget::backgroundRole())</code></p></li>
  <li><p><a href="https://stackoverflow.com/questions/17473525/how-to-get-background-color-of-a-dialog-window">How to get background color of a dialog/window?</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/1528685/qt-qplaintextedit-background/1532179#1532179">Qt QPlainTextEdit background</a></p></li>
  </ul>
  
  <p>There are at least 3 ways to solve the problem:</p>
  
  <pre><code>// 1st 
  QColor color = ui-&gt;toolButton-&gt;palette().color(QWidget::backgroundRole());
  // 2nd
  QColor color = ui-&gt;toolButton-&gt;palette().background().color();
  // 3rd
  QColor color = colorSetting = ui-&gt;toolButton-&gt;palette().color(QPalette::Window);
  </code></pre>
  
  <hr>
  
  <p><strong>Update:</strong> sorry I have made some mistakes, both of the following ways work well. </p>
  
  <hr>
  
  <p><strong>Raw Qusetion:</strong></p>
  
  <p>I have tried</p>
  
  <pre><code>QColor color = ui-&gt;toolButton-&gt;palette().background().color();
  </code></pre>
  
  <p>and </p>
  
  <pre><code>QColor color = colorSetting = ui-&gt;toolButton-&gt;palette().color(QPalette::Window);
  </code></pre>
  
  <p>both got <code>QColor(ARGB 1, 0.941176, 0.941176, 0.941176)</code>, not the right color I want.</p>
  
  <p>The background color is set by editing <code>mainwindow.ui</code>, change stylesheet of toolButton
  to <code>background-color: rgb(255, 170, 255);</code></p>
  
  <p><a href="https://i.stack.imgur.com/BqrFt.png" rel="nofollow noreferrer">image of my toolButton</a></p>
  
  <p>and for pyQt, see here <a href="https://stackoverflow.com/questions/17271366/how-to-get-the-background-color-of-a-button-or-label-qpushbutton-qlabel-in-py">How to get the background color of a button or label (QPushButton, QLabel) in PyQt</a></p>