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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on 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?

    VRoninV 1 Reply Last reply
    0
    • CJhaC CJha

      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?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      CJhaC 1 Reply Last reply
      0
      • VRoninV VRonin

        @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?

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on 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.

        JoeCFDJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          CJhaC 1 Reply Last reply
          1
          • CJhaC CJha

            @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.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on 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.

            CJhaC 1 Reply Last reply
            0
            • SGaistS SGaist

              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.

              CJhaC Offline
              CJhaC Offline
              CJha
              wrote on 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
              • JoeCFDJ JoeCFD

                @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.

                CJhaC Offline
                CJhaC Offline
                CJha
                wrote on last edited by
                #7

                @JoeCFD Hi, are you using style sheets?

                JoeCFDJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 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
                  • CJhaC CJha

                    @JoeCFD Hi, are you using style sheets?

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on 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

                    • Login

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