Passing a value to slot...
-
@Axel-Spoerl said in Passing a value to slot...:
The triggeredslot of QMenu passes the pointer to the triggered action, see here. I don't know why you expect it to pass a boolean. So even if the compiler would understand the broken syntax of the connect statement, the type would be wrong.
Exactly.
The requirement here, apart from the syntax error, is
(a) attach toQAction::triggered
signal (or maybe evenQAction::toggled
if it's a checkbox); and
(b) will requite a lambda for the slot if OP wants to pass an index to identify item pressed.Thanks all. I need to digest all of this.
I Think my main misunderstanding is confusing
monitoring signal (menu sender r ) triggered by bool (action) and same signal triggered by
value - index in my case .As of now it is a matter to put it all together...
-
Thanks all. I need to digest all of this.
I Think my main misunderstanding is confusing
monitoring signal (menu sender r ) triggered by bool (action) and same signal triggered by
value - index in my case .As of now it is a matter to put it all together...
@AnneRanch
CORRECTIONI am sorry I had wrong debug setting
This works
connect(subMenu[index] , &QMenu::triggered, this,&MainWindow_Bluetooth::testSlot);Can we all agree
subMenu[index] is sender and works
this is obviously OK
&MainWindow_Bluetooth::testSlot) is "receiver" OKthe problem is with SIGNAL
it should trigger from QAction "triggered" or " checked ".....I was wrong -
sender is QMenu and sends "triggered"
I need to work on "checked" and passing current index.
When I get the SIGNAL working I can work on detecting the sender index...
-
@AnneRanch
CORRECTIONI am sorry I had wrong debug setting
This works
connect(subMenu[index] , &QMenu::triggered, this,&MainWindow_Bluetooth::testSlot);Can we all agree
subMenu[index] is sender and works
this is obviously OK
&MainWindow_Bluetooth::testSlot) is "receiver" OKthe problem is with SIGNAL
it should trigger from QAction "triggered" or " checked ".....I was wrong -
sender is QMenu and sends "triggered"
I need to work on "checked" and passing current index.
When I get the SIGNAL working I can work on detecting the sender index...
@AnneRanch said in Passing a value to slot...:
Can we all agree
subMenu[index]
is senderYes.
and works
No.
The menu is just a visual tree that presents actions in a meaningful order. Actions like e.g. "Save file" can be presented in a menu (File->Save), and the same action can be triggered by a Save button on a menu bar. In that case, you don't want to connect to the menu and the button. You want to connect just to the action, because the application doesn't have to know how the action was triggered.
That said, the
QMenu::triggered
signal is more or less a helper. It tells you, that an action in the menu has been triggered. ItsQAction *action
argument tells you which one it was.
What probably goes wrong in your case: The action you want to react to, isn't located in the menu you connect it to. It's located in another submenu. That's why nothing happens.In my opinion, there is no need to keep your
QMenu
objects in an array. They are (hopefully) parented to some other widget, so they won't leak. It's better to keep track of yourQAction
pointers and connect them to the right slots.I am not going to refer you to the documentation again. @Christian-Ehrlicher has. Have you ever looked at it? An answer would be nice, but as usual we won't get it.
-
OK, I am getting of the subject,,,
I agree that QMenu SIGNAL is pretty limited - but I need to have WORKING code and I can tune / modify it later.
I was looking at mapping example and that was too convoluted...
I am now trying lambda and need code help with "
processAction() parameter .
The example uses "text" and I like to have the actual "index". :connect(subMenu[index] , &QMenu::triggered, this , [=]() { this->processAction() ); Here is the function definition snippet void MainWindow_Bluetooth::processAction(QAction ??? ) { #ifdef LAMBDA text = "TASK DEBUG connect... "; //connect text += Q_FUNC_INFO;
-
OK, I am getting of the subject,,,
I agree that QMenu SIGNAL is pretty limited - but I need to have WORKING code and I can tune / modify it later.
I was looking at mapping example and that was too convoluted...
I am now trying lambda and need code help with "
processAction() parameter .
The example uses "text" and I like to have the actual "index". :connect(subMenu[index] , &QMenu::triggered, this , [=]() { this->processAction() ); Here is the function definition snippet void MainWindow_Bluetooth::processAction(QAction ??? ) { #ifdef LAMBDA text = "TASK DEBUG connect... "; //connect text += Q_FUNC_INFO;
-
THANKS !
The code works...
But the index is wrong - it is final index of the main loop .... something to work on
I actually need both indexes - main and sub loop..
However - this lambda "connect" is the main and final code and that is a HUGE help - thanks -
THANKS !
The code works...
But the index is wrong - it is final index of the main loop .... something to work on
I actually need both indexes - main and sub loop..
However - this lambda "connect" is the main and final code and that is a HUGE help - thanks@AnneRanch
Theindex
passed here should be the one used in theconnect()
'ssubMenu[index]
, each one of those having its ownconnect()
passing its ownindex
value.subMenu[0]
should be passing0
,subMenu[1]
should be passing1
, etc.I don't know what the other thing is, but if you have another one, say i n variable,
other
, you can pass multiple parameters like:int other = 999; connect(subMenu[index] , &QMenu::triggered, this , [=]() { this->processAction(other, index); } ); void MainWindow_Bluetooth::processAction(int other, int index) { }
Yes, lambdas are the way to pass arbitrary values --- things in addition to anything the signal might pass --- to slots from your code. Syntax can get tricky, but they are very flexible/powerful.
-
@JonB said in Passing a value to slot...:
int other = 999;
connect(subMenu[index] , &QMenu::triggered, this ,
= { this->processAction(other, index); );Thanks again, you are very helpful with this post .
Minor detail;
you example is missing the closing
"}" - but the compiler catches that, no big deal. -
@AnneRanch
Theindex
passed here should be the one used in theconnect()
'ssubMenu[index]
, each one of those having its ownconnect()
passing its ownindex
value.subMenu[0]
should be passing0
,subMenu[1]
should be passing1
, etc.I don't know what the other thing is, but if you have another one, say i n variable,
other
, you can pass multiple parameters like:int other = 999; connect(subMenu[index] , &QMenu::triggered, this , [=]() { this->processAction(other, index); } ); void MainWindow_Bluetooth::processAction(int other, int index) { }
Yes, lambdas are the way to pass arbitrary values --- things in addition to anything the signal might pass --- to slots from your code. Syntax can get tricky, but they are very flexible/powerful.
@JonB Yes , I expect the sender to pass "its index", but I need to check why it is passing main loop final index. That is just a minor matter of cleaning up the code- now when it is running as expected.
Thanks very much for fixing the lambda "connect". -
@JonB said in Passing a value to slot...:
int other = 999;
connect(subMenu[index] , &QMenu::triggered, this ,
= { this->processAction(other, index); );Thanks again, you are very helpful with this post .
Minor detail;
you example is missing the closing
"}" - but the compiler catches that, no big deal.@AnneRanch said in Passing a value to slot...:
Minor detail;
you example is missing the closing
"}" - but the compiler catches that, no big deal.You are correct, my bad. I just type these examples in, my eyesight is not what it used to be! (I believe) I have corrected both my previous posts to have that missing
}
. -
A Axel Spoerl referenced this topic on