How to disable/grey QAction item text in Qt 4.8.4?



  • Hi All,

    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"));
    car->setEnabled(false);
    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()) );

    SampleClass::disableCarAction()
    {
    car->setEnabled(false);
    }

    @

    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.


  • Lifetime Qt Champion

    Hi,

    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



  • Hi SGaist,

    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.

    MyCustomWidget::createContextualMenu()
    {
    //code present in previous post.
    }
    @

    so it is pretty straight forward approach, I was not trying anythg unusual .


  • Lifetime Qt Champion

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.