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 know what's the current color background of a pushButton?
Forum Updated to NodeBB v4.3 + New Features

How to know what's the current color background of a pushButton?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.6k Views 2 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
    thiagorodrigo
    wrote on last edited by
    #1

    I put a pushButton like a toggle button and everytime a click(is. toggled), change the color to green and when I "distoggled" the color back to transparent.

    void gui_1::on_pushButton_4_clicked()
    {

    QPalette palette;
    palette.setColor(ui.pushButton_4->backgroundRole(), Qt::red);
    ui.pushButton->setAutoFillBackground(true);
    ui.pushButton_4->setPalette(palette);
    ui.pushButton_4->setCheckable(true);
    

    }
    if I just do this the button will be toggled only in the second clicked because call first the click, to solve this I called the clicked method in the main:
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    gui_1 w;
    w.show();

    w.on_pushButton_4_clicked();
    return a.exec();
    

    }

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

      Hi and welcome to devnet,

      Why not set the palette in the constructor of your widget ?

      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
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Why not set the palette in the constructor of your widget ?

        T Offline
        T Offline
        thiagorodrigo
        wrote on last edited by
        #3

        @SGaist why do I need this?
        I'm new using Qt C++, so..
        I could put the pushButton as a toggleButton, mas I need to change the color every time i toggle, but i couldn't, because I think I need to do something like:
        if(getColorBackgroung()==transparent){
        setColot(green)
        else
        setColor(transparent)

        something like that....

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Why not set the palette in the constructor of your widget ?

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

          @SGaist I need to know how can i get the current color to be able change the color....

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

            You rather need to know the state of your button. You should also be able to achieve this using style sheets

            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
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              As SGaist is saying:

              Using a style sheet is a bit easier as you do not need to keep track of state.
              As I understand it, you want to have a different color when "checked" ?

              If you right click a push button select "Change style sheet" and paste the following

              QPushButton {
              background-color: red;
              border-style: solid;
              border-width: 3px;
              border-radius: 75px 50px;
              border-color: blue;
              font: bold 14px;
              padding: 6px;
              }

              QPushButton:pressed {
              background-color: rgb(224, 0, 0);
              }

              QPushButton:checked {
              background-color: rgb(0, 0, 224);
              }

              you will have a push button that change color when checked
              (note colors are random and butt ugly :)

              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