Qt: Disable a Push Button without turning it gray - c++

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?
Thanks in advance!
[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.

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

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.

For disabling toolbutton and put icon of your choice instead of turning into grey
you have to load icon creating QPixmap object say qpm,
create QIcon object say icon
icon.addPixmap(qpm,QIcon::Disabled,QIcon::On) this line will show icon
setIcon on toolbutton
setStyleSheet
When enabling Toolbutton just change
3. icon.addPixmap(qpm,QIcon::Normal,QIcon::On)

Related

Close button on QTabWidget not the Tabs in the QTabWidget

I am using Qt-5.8 over Ubuntu.
This is how my QTabWidget appears :
And the black dot is where I want the Minimize Button to happen.
One way I came across is that I can use QToolButton *qToolButton to create a new button and tabWidget->setCornerWidget(qToolButton) and then add the implementation over its click event.
But should there not be any other way to just show the minimize button as like in MainWindow or SubWindows has. Which just minimizes it.
Minimize button on top panel of your QMainWindow's instance is part of Windows Manager subsystem of your OS. So you can't use similar approach inside your window as toolbox with buttons, etc.
As you wrote, try to use tabWidget->setCornerWidget(qToolButton) to place your custom minimize button inside your window.

Toggle button in MFC

How to make a toggle button in MFC dialog,?
Like the one you usually used in switching on the wifi in smart phone, push like switch button and radion buttons are not my needs,
Uptil now i reached to changing the switch button to push like effect but i need the real toggle button effect as describe about those in smartphones.
Remember that it supports dragging to on from off and vice versa.... :(
You can keep 2 images that are visible when button is pressed and one when button is not pressed. Now you can use CBitmapButton::LoadBitmaps or CButton::SetBitmap to change the image everytime the button is clicked. Make sure to invalidate the button so that new image can take effect.
//load your bitmaps (in constructor if dialog)
m_wifionBitmap.LoadBitmap(IDB_WIFION);
m_wifioffBitmap.LoadBitmap(IDB_WIFIOFF);
// In turn_on_wifi()
CButton* pButton = (CButton*)GetDlgItem(IDC_WIFI_TOGGLE_BUTTON);
pButton->SetBitmap(HBITMAP)m_wifionBitmap);
// In turn_off_wifi()
CButton* pButton = (CButton*)GetDlgItem(IDC_WIFI_TOGGLE_BUTTON);
pButton->SetBitmap(HBITMAP)m_wifioffBitmap);

How to add widgets positioned relative to a tab in a QTabBar?

Is it possible to add some widgets to a QTabBar? I wanted to have a QComboBox to the side of the last tab, and have it only appear when the last tab is selected.
It's possible to add child overlay widgets to any widget, so the answer is: sure!
You can hook to the tab widgets's or tab bar's signals to get notified when the last tab is selected. Then use tabRect() to get the rectangle of the last tab. Position your combo box to the right of it. It'd need to be a child of the tab bar. That's it.
It might be easier to use a QStackedWidget to get your desired results. When you are using the QStackedWidget you can have different buttons outside that reveal the different widgets. Then use some custom signal for when the last button is activated to show a combobox that appears next to the last button.
Here is the link to the QStackedWidget

How to remove the "default button" border?

When you have a GtkButton in GTK and it is the default for some window (will be activated when you press Enter), a tiny border is draw around it to inform the user that the button is the default button. How to remove this border ?
I already tried: default-border and default-outside-border style properties.
After hours finding for this, I've discovered it:
gtk_button_set_relief(GTK_BUTTON(button_widget), GTK_RELIEF_HALF);
GTK should have a gallery showing how this changes the button behavior.

How to SetFocus to a CButton so that the border and focus dotted line are visible?

I created a simple dialog-based application, and in the default CDialog added three buttons (by drag-and-dropping them) using the Visual Studio editor.
The default OK and Cancel buttons are there too.
I want to set the focus to button 1 when I click button 3.
I set the property Flat to true in the properties for muy buttons.
I coded this:
void CbuttonfocusDlg::OnBnClickedButton3()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_BUTTON1)->SetFocus();
Invalidate();
}
But the boder in button1 is never drawn. The caret (the dotted line indicating focus) is only drawn if I pressed TAB any time before clicking button 3.
I want the button to look exactly as it looks after I click it. Showing the dotted line inside the button programatically, would be a plus.
What I want:
What I get:
Use WM_NEXTDLGCTL.
See Reymond Chen's "How to set focus in a dialog box":
void SetDialogFocus(HWND hdlg, HWND hwndControl)
{
SendMessage(hdlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, TRUE);
}
By calling UpdateWindow, the button is being redrawn before the focus change can take effect. The Invalidate should be sufficient by itself, the window will get repainted when everything settles down.
This draws the thick border around the button:
static_cast<CButton*>(GetDlgItem(IDC_BUTTON1))->SetButtonStyle(BS_DEFPUSHBUTTON);
A more elegant way to do this would be to define a CButton member variable in CbuttonfocusDlg and associate it to the IDC_BUTTON1 control, and then calling
this->m_myButton.SetButtonStyle(BS_DEFPUSHBUTTON);
This makes the button to which I'm setting the focus the default button, but note that when the focus goes to a control (inside the dialog) that is not a button, the default button is once more the original default button set in the dialog resource, in this case the "Ok" button.
I am following Joel's suggestion. But slightly different with the API used in that link, my one is :
PostMessage(WM_NEXTDLGCTL, (WPARAM)(pwnd->GetSafeHwnd()), TRUE);