Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems with QMenu::addMenu
Forum Updated to NodeBB v4.3 + New Features

Problems with QMenu::addMenu

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 7.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sebi2020
    wrote on last edited by
    #1

    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");
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Welcome to devnet

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sebi2020
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Thanatos.jsse
            wrote on last edited by
            #5

            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);@

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sebi2020
              wrote on last edited by
              #6

              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*)
              @

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sebi2020
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sebi2020
                    wrote on last edited by
                    #9

                    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 :)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved