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 define push button pressed colour through QPalette in Fusion Style?

How to define push button pressed colour through QPalette in Fusion Style?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpaletteqpushbutton
9 Posts 4 Posters 1.5k 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.
  • C Offline
    C Offline
    CJha
    wrote on 28 Jun 2021, 13:10 last edited by
    #1

    Is there any way to change the QPushButton pressed colour through QPalette in Fusion style? My application depends on the colour of QPushButton to indicate its current state. However, the change in colour between the pressed and non-pressed state of QPushButton is almost negligible, it's very hard to say if a QPushButton is pressed or not because except for a very slight change in the button colour, nothing else changes. Changing the colour of each QPushButton when it is pressed is a big task. QPalette has things like QPalette::Base and QPalette::AlternateBase and so many other options but I can't seem to figure out what palette combination (i.e. ColorGroup and ColorRole) is used for QPushButton pressed state?

    V 1 Reply Last reply 28 Jun 2021, 13:24
    0
    • C CJha
      28 Jun 2021, 13:10

      Is there any way to change the QPushButton pressed colour through QPalette in Fusion style? My application depends on the colour of QPushButton to indicate its current state. However, the change in colour between the pressed and non-pressed state of QPushButton is almost negligible, it's very hard to say if a QPushButton is pressed or not because except for a very slight change in the button colour, nothing else changes. Changing the colour of each QPushButton when it is pressed is a big task. QPalette has things like QPalette::Base and QPalette::AlternateBase and so many other options but I can't seem to figure out what palette combination (i.e. ColorGroup and ColorRole) is used for QPushButton pressed state?

      V Offline
      V Offline
      VRonin
      wrote on 28 Jun 2021, 13:24 last edited by
      #2

      @CJha said in How to define push button pressed colour through QPalette in Fusion Style?:

      My application depends on the colour of QPushButton to indicate its current state

      This is a terrible idea and should be changed. Any reason why you don't get the state directly?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      C 1 Reply Last reply 28 Jun 2021, 13:32
      0
      • V VRonin
        28 Jun 2021, 13:24

        @CJha said in How to define push button pressed colour through QPalette in Fusion Style?:

        My application depends on the colour of QPushButton to indicate its current state

        This is a terrible idea and should be changed. Any reason why you don't get the state directly?

        C Offline
        C Offline
        CJha
        wrote on 28 Jun 2021, 13:32 last edited by
        #3

        @VRonin It's an application that controls hardware and the hardware can be in different states. I use foreground colour (i.e. button colour) to indicate the current state of the hardware. Even if I don't use the colours to indicate the change in hardware state, still the change between the pressed and un-pressed state of the button in fusion style is negligible and this can cause confusion for users.

        J 1 Reply Last reply 28 Jun 2021, 21:31
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 28 Jun 2021, 18:55 last edited by
          #4

          Hi,

          What kind of state are your button representing ?
          It might not be the best control for that. Maybe the use of icons would make things clearer.

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

          C 1 Reply Last reply 29 Jun 2021, 07:54
          1
          • C CJha
            28 Jun 2021, 13:32

            @VRonin It's an application that controls hardware and the hardware can be in different states. I use foreground colour (i.e. button colour) to indicate the current state of the hardware. Even if I don't use the colours to indicate the change in hardware state, still the change between the pressed and un-pressed state of the button in fusion style is negligible and this can cause confusion for users.

            J Online
            J Online
            JoeCFD
            wrote on 28 Jun 2021, 21:31 last edited by JoeCFD
            #5

            @CJha I am using background color for this. Different states can be defined for button events(Enter, pressed, released, etc). Simply attach different background colors to the states. If the states change too fast, use timer to add a bit delay while hardware change is much slower.

            C 1 Reply Last reply 29 Jun 2021, 07:55
            0
            • S SGaist
              28 Jun 2021, 18:55

              Hi,

              What kind of state are your button representing ?
              It might not be the best control for that. Maybe the use of icons would make things clearer.

              C Offline
              C Offline
              CJha
              wrote on 29 Jun 2021, 07:54 last edited by
              #6

              @SGaist My buttons are representing hardware states of equipment that is used for electrophysiological measurements and is going to be used in labs and industry. My application controls hardware which can be in different states, the controls in these different states remain almost the same, but their functionality changes because of the state change of the hardware. To make these changes clear to the user I am using different colours for the QPushButton.

              Even if I keep the same colour for the QPushButton for all the states of the hardware, still the change in pressed vs un-pressed state of the QPushButton in Fusion style is very small and can be easily missed. This can lead to confusion in users mind as to if a button is currently pressed or not. Since it is a scientific/industrial application there is no room for any kind of confusion. I do not want to use WindowsVista or MacOS style as these are very restrictive, especially if the background is dark then the button colour doesn't change and it remains light coloured but its text changes to a light colour making all the button text unreadable. I have to implement dark mode in my application because it is also going to be used in the darkroom (fluorescent imaging, etc).

              All I am looking for is a way to have a proper distinguishing factor between button pressed vs un-pressed state. Previously, I had implemented this by using a singleton in my application and then doing the following on my application startup (after setupui is called in QMainWindow, Sole is my singleton class object):

              void Sole::connectAllPushButtonsToSlotsInSole()
              {
                  QWidgetList allWidgets = qApp->allWidgets();
                  for(QWidget* wid : allWidgets) {
                      QString str = wid->metaObject()->className();
                      if(str == "QPushButton") {
                          QPushButton* btn = dynamic_cast<QPushButton*>(wid);
                          bool chkbl = btn->property("checkable").toBool();
                          if(chkbl) {
                              connect(btn, &QPushButton::toggled, this, &Sole::priS_pushButtonToggled);
                          }
                          else {
                              connect(btn, &QPushButton::pressed, this, &Sole::priS_pushButtonPressed);
                              connect(btn, &QPushButton::released, this, &Sole::priS_pushButtonReleased);
                          }
                      }
                  }
              }
              
              void Sole::priS_pushButtonPressed()
              {
                  QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender());
                  sender->setPalette(darkBtnPal);
                  QFont font = sender->font();
                  font.setItalic(true); //set font to italic
                  sender->setFont(font);
              }
              
              void Sole::priS_pushButtonReleased()
              {
                  QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender());
                  sender->setPalette(lightBtnPal);
                  QFont font = sender->font();
                  font.setItalic(false); //set font to normal (i.e. undo italic)
                  sender->setFont(font);
              }
              
              void Sole::priS_pushButtonToggled(bool toggled)
              {
                  QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender());
                  if(toggled) {
                      sender->setPalette(darkBtnPal);
                      QFont font = sender->font();
                      font.setItalic(true); //set font to italic
                      sender->setFont(font);
                  }
                  else {
                      sender->setPalette(lightBtnPal);
                      QFont font = sender->font();
                      font.setItalic(false); //set font to normal (i.e. undo italic)
                      sender->setFont(font);
                  }
              }
              

              Given my recent experience with Singletons, I am trying to get rid of it and so I am looking for a new way to achieve the same level of distinguishing factor between pressed vs un-pressed state of QPushButton.

              1 Reply Last reply
              0
              • J JoeCFD
                28 Jun 2021, 21:31

                @CJha I am using background color for this. Different states can be defined for button events(Enter, pressed, released, etc). Simply attach different background colors to the states. If the states change too fast, use timer to add a bit delay while hardware change is much slower.

                C Offline
                C Offline
                CJha
                wrote on 29 Jun 2021, 07:55 last edited by
                #7

                @JoeCFD Hi, are you using style sheets?

                J 1 Reply Last reply 29 Jun 2021, 21:08
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 29 Jun 2021, 19:55 last edited by
                  #8

                  Not knowing how the application looks like I can't really comment on your use of color.

                  However, I still encourage you to consider using icons to convey more information to your users.

                  QIcon supports sets of images for different states that may make your application easier to read and follow.

                  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
                  • C CJha
                    29 Jun 2021, 07:55

                    @JoeCFD Hi, are you using style sheets?

                    J Online
                    J Online
                    JoeCFD
                    wrote on 29 Jun 2021, 21:08 last edited by
                    #9

                    @CJha Since my buttons change icons for different states as well, they are redrawn. If only background color needs to change, reset stylesheet is good enough in your case..

                    1 Reply Last reply
                    0

                    5/9

                    28 Jun 2021, 21:31

                    • Login

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