How to disable/grey QAction item text in Qt 4.8.4?
I am creating a QMenu with a set of QAction items as menuitems. My intention is to disable one of the menu items. I am using QAction's setEnabled(false) to disable the item but I find that the action item text is not greying out at all.
QMenu *contextual_menu = QMenu(this);
QAction *car = contextual_menu ->addAction(QString("car"));
QAction bus = contextual_menu ->addAction(QString("bus"));
QAction selected_item = contextual_menu ->exec(QCursor::pos());
My expectation is that the action item "car" should be displayed in greyed state which is not happening, although clicking on it does not return the selected item but clicking on "bus" returns the selected item.
Can smeone pls let me know whats the possible flaw in my code? I find it very amusing and baffling that such simple code didn't yield the desired result. any help is appreciated.
I have also tried the other way using the QMenu's aboutToShow() signal.
connect( contextual_menu, SIGNAL(aboutToShow()), this, SLOT(disableCarAction()) );
My intention is to show one of the action items in greyed/disabled state when I click on a button which displays a contextual menu with a list of QAction item's.
Can you provide a minimal sample code that allows to reproduce the problem ? Not just the QMenu part (which you already provided) but a widget that one can directly use to test
The issue can be reproduced by creating a QPushButton on a QWidget and then connecting the button's signal clicked() to a slot called createContextualMenu(). In the slot method we can write the menu creation part with a set of QActions as menu items.
I just used Qt Designer to create a Qwidget with a Qpushbutton in it.
QPushButton *more = new QPushButton();
connect(more, SIGNAL( clicked()), this, SLOT( createContextualMenu());
//this object can be any child of QWidget.
//code present in previous post.
so it is pretty straight forward approach, I was not trying anythg unusual .
What os are you running on ? At least on OS X it's behaving correctly
@SGaist - I'm using Windows 7 with Qt 4.8.4