Passing parmaterts to slot - what are the options ?
-
int index = 0; QAction *TESTMenu_aboutAct = BTMenu->addAction(tr("&Run btscanner "), // this, &MainWindow::menu_slot_indexed(0);); this, [this]{menu_slot_indexed(0);}); works this, [this]{menu_slot_indexed(index);}); does not compile // NOTE will not allow to pass index as variable TESTMenu_aboutAct->setStatusTip(tr("Run bluetooth (excample ) btscanner menu_slot")); TOK
The above has been posted as a solution to pass parameter to slot , sort off "half lambda" - BUT the (temporary fixed) parameter "o" cannot be passed as variable.
The intent is to have fixed menu options - index being as convenient addition distinction in the menus.The pseudo code will look something like this
QAction *TESTMenu_aboutAct = BTMenu->addAction(tr("&Run btscanner "),
// this, &MainWindow::menu_slot_indexed(0););
QAction *TESTMenu_aboutAct = BTMenu->addAction(tr("&Run appliication xr "),
// this, &MainWindow::menu_slot_indexed(1););
QAction *TESTMenu_aboutAct = BTMenu->addAction(tr("&Run application y "),
// this, &MainWindow::menu_slot_indexed(2););It makes the actual action goofy and almost defeats the notion to have a common, indexed slot function.
Is there any other way to "pass parameter" to slot - besides using "index" as global variable?
- therefore not passed a slot parameter
-
@AnneRanch there is no such thing as a "half lambda", please take your time and study the official source
https://en.cppreference.com/w/cpp/language/lambda
or study book or something to get a better concept of the stuff you use.
Is there any other way to "pass parameter" to slot - besides using "index" as global variable?
yes, capturing by copy.
this, [this, index]()->void{menu_slot_indexed(index);});
-
@AnneRanch
Sorry - I quoted a source , I did not invent the "half lambda".
nothing to feel sorry about.
I actually may need to pass more then just index...
Ehm, your example showed how to capture one variable
this
I showed you how to capture 2 variables -this
andindex
- and gave you a link to the documentation....You know, there are two types of people in this world:
- Those who can extrapolate from incomplete data