QMenu with tearoff enabled and with QWidgetAction



  • Implemented QMenu with QWidgetAction as one of the menu item along with many QAction's in it. Also enabled the tear off (setTearOffEnabled(true) ).

    On executing the app and doing tearoff of menu, this QWidgetAction (which is a QLabel) is blank. Is this a bug in QT? If yes, is there any alternative to make the QWidgetAction visible?

    Thanks.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ? On which OS ?

    Can you share the code where you setup that menu ?



  • Thanks SGaist for replay.
    I took the example itself from Qt : examples\activeqt\menus\main.cpp, menus.cpp

    I updated menus.cpp and added following code:

    QMenus::QMenus(QWidget *parent) 
        : QMainWindow(parent, 0) 
    {
        QAction *action;
    
        QMenu *file = new QMenu(this);
        file->setTearOffEnabled(true);  //<= MY CHANGE
    
        action = new QAction(QPixmap((const char**)fileopen), "&Open", this);
        action->setShortcut(tr("CTRL+O"));
        connect(action, SIGNAL(triggered()), this, SLOT(fileOpen()));
        file->addAction(action);
    
        action = new QAction(QPixmap((const char**)filesave),"&Save", this);
        action->setShortcut(tr("CTRL+S"));
        connect(action, SIGNAL(triggered()), this, SLOT(fileSave()));
        file->addAction(action);
                                                                        //<= MY CHANGE
        file->addSeparator();                                           //<= MY CHANGE
        QWidgetAction* waction1 = new QWidgetAction(this);              //<= MY CHANGE
        QLabel*        label  = new QLabel("My Data");                  //<= MY CHANGE
        label->setAlignment(Qt::AlignHCenter);                          //<= MY CHANGE
        waction1->setDefaultWidget(label);                              //<= MY CHANGE
        file->addAction(waction1);                                      //<= MY CHANGE
                                                                        //<= MY CHANGE
        QWidgetAction* waction2 = new QWidgetAction(this);              //<= MY CHANGE
        QCheckBox*        chk  = new QCheckBox("Personal Data");        //<= MY CHANGE
        waction2->setDefaultWidget(chk);                                //<= MY CHANGE
        file->addAction(waction2);                                      //<= MY CHANGE
    
    

    So, after tearoff, Label and checkbox are missing/hidden. Further I found following defect which looks to be not addressed yet.
    https://bugreports.qt.io/browse/QTBUG-1017

    I tried 4.8.6 and 5.5. Is there any way out for this?


Log in to reply
 

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