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. QPushButtons color
Forum Update on Monday, May 27th 2025

QPushButtons color

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 5.7k Views
  • 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.
  • Z Offline
    Z Offline
    zheka777
    wrote on 25 Oct 2013, 13:28 last edited by
    #1

    Hey all,

    I designed a Gui by the Qt Gui Designer and I have Toogle Buttons.
    !http://abload.de/img/bildq2s1l.jpg(picture)!

    Now I want to say:
    If the Toogle Button @setChecked(false) THEN Color = RED@
    If the Toogle Button @setChecked(true) THEN Color = GREEN@

    I tryed to implement a function:
    @void MainWindow::setFarbe_r() {
    ui->pushButton->setStyleSheet("background-color:red;");@
    and the Connect:
    @connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(setFarbe_r()));@
    but it doesnt work.

    What I also could imagine that under the button would be displayed a textline.
    For example when the button is pressed -> text: "button is pressed"

    Thank you for Help

    Hochschule Kempten

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Oct 2013, 19:37 last edited by
      #2

      Hi,

      You can use the pseudo states :checked and :unchecked in your style sheet to set the button background colors

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tabi
        wrote on 26 Oct 2013, 09:18 last edited by
        #3

        I think you need "this":http://doc.qt.digia.com/4.7/stylesheet-examples.html#customizing-qpushbutton

        HTH

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zheka777
          wrote on 28 Oct 2013, 07:31 last edited by
          #4

          [quote author="SGaist" date="1382729840"]Hi,

          You can use the pseudo states :checked and :unchecked in your style sheet to set the button background colors[/quote]

          Thanks for answer. But what do you mean? Can you explain your answer please.. Where shoud i use this pseudo states? And how?
          Can you write an example please?

          Hochschule Kempten

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 7 Nov 2013, 07:55 last edited by
            #5

            The doc pointed by Tabi gives an example using the pressed state, it's the same for checked and unchecked

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              guru
              wrote on 7 Nov 2013, 08:15 last edited by
              #6

              HI,

              You just implement a slot through designer and follow your first method like this..
              @ void MainWindow::setFarbe_r(bool checked) {
              if(checked)
              ui->pushButton->setStyleSheet("background-color:red;");
              else
              ui->pushButton->setStyleSheet("background-color:green;");
              }
              @

              1 Reply Last reply
              0
              • I Offline
                I Offline
                IamSumit
                wrote on 7 Nov 2013, 10:16 last edited by
                #7

                Hello zhekka777,
                Why don't u use "toggled slot of QPushButton "provided by Qt in order to behave as a toggle button..I have used it to make a toggle button .
                First drag and drop a QPushButton on MainWindow and set it's property "checkable" to true and go to slot of QPushButton choose signal toggled(bool).
                Write the following snippet of code inside this slot.
                void MainWindow::on_pushButton_toggled(bool checked)
                {

                if(checked==false)
                
                {
                
                     ui->pushButton->setStyleSheet("QPushButton{ background-color :red; }");
                
                    ui->pushButton->setText("Btn is not Pressed");  //optional
                
                }
                

                else

                {
                
                
                ui->pushButton->setStyleSheet("QPushButton{ background-color :green; }");
                ui->pushButton->setText("Btn is Pressed");  //optional
                
                
                }
                

                }

                Be Cute

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NicuPopescu
                  wrote on 7 Nov 2013, 11:54 last edited by
                  #8

                  pressed state is for mouse button pressed ...

                  the correct QSS for checked and unchecked(toggling states) are:

                  @QPushButton:on
                  {
                  background-color:rgba(255,0,0,255);
                  }

                  QPushButton:off
                  {
                  background-color:rgba(0,0,255,255);
                  }@

                  pushButton->setStyleSheet(the wole string in code above) or set it in designer

                  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