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. Change background color of QPushButton at runtime
Qt 6.11 is out! See what's new in the release blog

Change background color of QPushButton at runtime

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 750 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.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by
    #1

    Hello, I need to change a background color of QPushButton during execution, so I created this StyleSheet:

    QPushButton {
    	background-color:rgb(255, 255, 255);
    	font-weight:bold;
    	font-size:24px;
    	color:#000;
    }
    
    *[class=active] {
    	background-color:rgb(255, 0, 0);
    	font-weight:bold;
    	font-size:24px;
    	color:#000;
    }
    

    and in code I call:

    ui->pushButton_Click->setProperty("class", "active");
    

    But it works only if it is in

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    

    function, if I call it in my custom function it doesn't work.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you applying the technique described in this Wiki entry ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 2 Replies Last reply
      2
      • SGaistS SGaist

        Hi,

        Are you applying the technique described in this Wiki entry ?

        T Offline
        T Offline
        t0msk
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Are you applying the technique described in this Wiki entry ?

          T Offline
          T Offline
          t0msk
          wrote on last edited by
          #4

          @SGaist thank you so much, it works, I forgot to use unpolish and polish.

          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