JO_
JO_

Reputation: 574

Qt: Disable a Push Button without turning it gray

I need to make a checkable Push Button in Qt 4.8 that when is checked it becomes disabled.

The problem that I have is that the button turns gray and I need to keep it with the same color always. I have two questions for two possibles paths to follow:

  1. Is there a way to disable the gray out effect when I use button.setEnabled(false)?

  2. Is there a way to hook the click event so I can "simulate" the disabled property?

[Edit] To give a little context, I have two push buttons that should toggle each other and that's why I need to prevent clicking on a pressed button.

Upvotes: 1

Views: 4237

Answers (4)

crubow
crubow

Reputation: 1

Using a stylesheet you can specify the colors you want when the button is in a disabled state, e.g., myBtn.setStyleSheet("QPushButton:disabled{background-color: mycolor;}).

Upvotes: 0

ajitem kudtarkar
ajitem kudtarkar

Reputation: 1

For disabling toolbutton and put icon of your choice instead of turning into grey

  1. you have to load icon creating QPixmap object say qpm,
  2. create QIcon object say icon
  3. icon.addPixmap(qpm,QIcon::Disabled,QIcon::On) this line will show icon
  4. setIcon on toolbutton
  5. setStyleSheet

When enabling Toolbutton just change 3. icon.addPixmap(qpm,QIcon::Normal,QIcon::On)

Upvotes: 0

hate-engine
hate-engine

Reputation: 2350

To give a little context, I have two push buttons that should toggle each other and that's why I need to prevent clicking on a pressed button.

You should use QButtonGroup instead.

The only way that the button should be unchecked is when the user checks the other button...

From doc:

In an exclusive group, the user cannot uncheck the currently checked button by clicking on it; instead, another button in the group must be clicked to set the new checked button for that group.

Upvotes: 0

JustMaximumPower
JustMaximumPower

Reputation: 1277

Try button.blockSignals(true). You could also overwrite the look of the button when its disabled with a Qt style sheet.

Upvotes: 3

Related Questions