Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to pass paramaters to "addAction"
Qt 6.11 is out! See what's new in the release blog

How to pass paramaters to "addAction"

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 3 Posters 1.5k Views 2 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    I am lost again in C++.
    My function is declared as such :
    Run_TAB (int )
    and I am getting this error

    /media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 4 WORKCOPY 1/CAT/mdi/mainwindow.cpp:682: error: lvalue required as unary ‘&’ operand
    ../../../JUNE 4 WORKCOPY 1/CAT/mdi/mainwindow.cpp: In member function ‘void MainWindow::createActions()’:
    ../../../JUNE 4 WORKCOPY 1/CAT/mdi/mainwindow.cpp:682:69: error: lvalue required as unary ‘&’ operand
    682 | this, &MainWindow::Run_TAB(selection));
    | ~~~~~~~~~^

    And it looks as Run_TAB is passed to addAction as
    QKey Sequence.
    I do not get that at all .

    It woks fine when Run_TAB is declared as (void) - no parameters.

    template<typename Functor>
    QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0);
    template<typename Functor>
    QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
    template<typename Functor>
    QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0);
    template<typename Functor>
    QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
    #else

    Here is my implementation which gives the error.

    QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth  Development (TAB) "),
                                          this, &MainWindow::Run_TAB(selection));
    
    sierdzioS 1 Reply Last reply
    0
    • A Anonymous_Banned275

      I am lost again in C++.
      My function is declared as such :
      Run_TAB (int )
      and I am getting this error

      /media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 4 WORKCOPY 1/CAT/mdi/mainwindow.cpp:682: error: lvalue required as unary ‘&’ operand
      ../../../JUNE 4 WORKCOPY 1/CAT/mdi/mainwindow.cpp: In member function ‘void MainWindow::createActions()’:
      ../../../JUNE 4 WORKCOPY 1/CAT/mdi/mainwindow.cpp:682:69: error: lvalue required as unary ‘&’ operand
      682 | this, &MainWindow::Run_TAB(selection));
      | ~~~~~~~~~^

      And it looks as Run_TAB is passed to addAction as
      QKey Sequence.
      I do not get that at all .

      It woks fine when Run_TAB is declared as (void) - no parameters.

      template<typename Functor>
      QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0);
      template<typename Functor>
      QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
      template<typename Functor>
      QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0);
      template<typename Functor>
      QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
      #else

      Here is my implementation which gives the error.

      QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth  Development (TAB) "),
                                            this, &MainWindow::Run_TAB(selection));
      
      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @AnneRanch said in How to pass paramaters to "addAction":

      &MainWindow::Run_TAB(selection)

      This is a "pointer to function", similar to what you will find in modern connect statement:

      connect(this, &Object::foo,
          that, &Object::bar)
      

      This only tells the function what function to call on which object. But it does not tell it what parameters to pass.

      And so the same rules apply: you cannot pass parameters in this call - it should be:

      QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth  Development (TAB) "),
                                            this, &MainWindow::Run_TAB);
      

      If you need to pass some concrete data, you can use a lambda:

      QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth  Development (TAB) "),
                                            this, [=]() {
          this->Run_TAB(selection);
      });
      

      (Z(:^

      1 Reply Last reply
      5
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        std::bind also works:

        QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth  Development (TAB) "),
                                              this, std::bind(&MainWindow::Run_TAB,this,selection));
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1

        • Login

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