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. How to change a Button colour
QtWS25 Last Chance

How to change a Button colour

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 81.9k 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.
  • T Offline
    T Offline
    TheDestroyer
    wrote on last edited by
    #1

    Hello guys,

    I searched everywhere for the answer to this question. But the answers I found are either too confusing or don't work. I want a single serial code to change the color of a button while the window is running.

    I have an OpenGL application, and I want the colour to be taken from a QColorDialog and then colour the background of my OpenGL scene, and meanwhile colour the button to show the user what colour chosen (I think you know what I mean).

    Here's the code I'm currently using. The code colours the background behind the button. I want the button itself to be coloured or to have a small section coloured inside it, like we see in all painting programs.

    @
    QPushButton *backgroundColourButton;
    QColor *backgroundColour;
    backgroundColour->setRgb(0,0,0); //example colour, I could later manage to utilize this variable with a QColorDialog
    QPalette Pal(palette());
    Pal.setColor(QPalette::Button, backgroundColour->rgb());
    backgroundColourButton->setAutoFillBackground(true);
    backgroundColourButton->setPalette(Pal);
    @

    Thank you for any efforts :)

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Easiest way would be to use a style sheet on the button:

      @
      backgroundColourButton->setStyleSheet("background-color: red");
      @

      BTW - according to a "talk of Girish Ramakrishnan":/videos/watch/styling_qt_widgets_using_style_sheets (one of the original developers of Qt widget style sheets) at DevDays 2006, it was exactly the need of "making a red push button" that led to the introduction of Qt style sheets eventually :-)

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TheDestroyer
        wrote on last edited by
        #3

        But is it possible to get the colour as string from a QColor object? this would solve my problem!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Of course:

          @
          QColor col = QColorDialog::getColor(Qt::white, this);
          if(col.isValid()) {
          QString qss = QString("background-color: %1").arg(col.name());
          ui->pushButton->setStyleSheet(qss);
          }
          @

          Although, the style of the button changes for me, i.e. the rounded box on the Mac is replaced by a plain rectangle.

          http://www.catb.org/~esr/faqs/smart-questions.html

          P 1 Reply Last reply
          0
          • T Offline
            T Offline
            TheDestroyer
            wrote on last edited by
            #5

            Thanks a lot :-)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              No prob, you're welcome!

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G goetz

                Of course:

                @
                QColor col = QColorDialog::getColor(Qt::white, this);
                if(col.isValid()) {
                QString qss = QString("background-color: %1").arg(col.name());
                ui->pushButton->setStyleSheet(qss);
                }
                @

                Although, the style of the button changes for me, i.e. the rounded box on the Mac is replaced by a plain rectangle.

                P Offline
                P Offline
                Pipevasquez2
                wrote on last edited by
                #7

                @goetz said:

                backgroundColourButton

                como cambio el color de un button al darle click?

                mrjjM 1 Reply Last reply
                0
                • P Pipevasquez2

                  @goetz said:

                  backgroundColourButton

                  como cambio el color de un button al darle click?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Pipevasquez2
                  Hi This is english section, but guessing from google translate

                  QPushButton:pressed {
                  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #0d5ca6, stop: 1 #2198c0);
                  }

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved