Unsolved How to pass paramaters to "addAction"
-
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);
#elseHere is my implementation which gives the error.
QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth Development (TAB) "), this, &MainWindow::Run_TAB(selection));
-
@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); });
-
std::bind
also works:QAction *CAT_TAB = CATMenu->addAction(tr("&Bluetoooth Development (TAB) "), this, std::bind(&MainWindow::Run_TAB,this,selection));