Problems with QMenu::addMenu



  • Does anybody know why the QMenu Class consists only of four addActions? I can't add an menu entry with addAction(QAction action).
    Only with direct typing like:
    @
    menu->addAction("&File");
    @


  • Moderators

    Welcome to devnet

    Do you have any particular reason not using "addMenu":http://developer.qt.nokia.com/doc/qt-4.8/qmenu.html#addMenu ?



  • There is no such <code>QMenu::addAction(QAction action)</code> method because <code>QAction</code> is a <code>QObject</code> and thus cannot be passed as value.

    The method you are looking for is <code>QMenu::addAction(QAction* action)</code>, which allows you to pass an already existing <code>QAction</code>. Be aware that the <code>QAction</code> has to be created on the heap. Do not pass a stack object!



  • @koahnig
    Because I don't want a sub-menu. I want to create an menu-item.

    @Lukas Geyer
    Sorry, I mean QMenu::addAction(QAction* action).
    In this QMenu class I can't find this one.



  • Try something like this:

    @QAction *myAction = new QAction(tr("&File"));
    myAction->setShortcuts("Ctrl+F");
    myAction->setStatusTip("Whatever you want...");
    connect(myAction,SIGNAL(triggered()), this, SLOT(myFileFunction()));

    // ...

    menu->addAction(myAction);@



  • If i call the QAction constructor in this way:
    @
    QAction aktOpen = new QAction(QObject::tr("&File"));
    @
    i get the following error message:
    @
    main.cpp:11: error: no matching function for call to 'QAction::QAction(QString)'
    ../../../QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui/qaction.h:236: note: candidates are: QAction::QAction(const QAction&)
    ../../../QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui/qaction.h:212: note: QAction::QAction(QActionPrivate&, QObject
    )
    ../../../QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui/qaction.h:103: note: QAction::QAction(const QIcon&, const QString&, QObject*)
    ../../../QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui/qaction.h:102: note: QAction::QAction(const QString&, QObject*)
    ../../../QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui/qaction.h:101: note: QAction::QAction(QObject*)
    @



  • The funny thing is that I sometimes can use a const char * instand of a a QString* and sometimes i get an error message if I use a const char .
    As an Example QMenu::QMenu want a QString
    but if I put there a const char * it works.



  • The signature of the "QAction constructor taking a QString as argument":/doc/qt-4.8/qaction.html#QAction-2 is quite clear:

    @
    QAction::QAction ( const QString & text, QObject * parent )
    @

    The parent pointer is not optional, you have to pass a a value here. So you're not running into some const char * or QString issue, but a missing second argument here.



  • Oh, and now everything is working. Oh sorry... was my fault... But the suggestions from Qt-Creator have no functions with the QAction* action signature ( QMenu::QMenu ). But it works :)


Log in to reply
 

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