help to find "connect" error
-
Do you really think spamming us with the same question over and over again will help you in any way? Maybe starting with basic c++ stuff would be a much better option.
-
This post is deleted!
-
As long as you won't format your code correctly noone can read your code.
-
@AnneRanch said in help to find "connect" error:
connect(subMenu[index], &QMenu::triggered, this ,= { this->processMenu(index,index_sub) ;});
It's hard to know what you are saying here, what you are surprised at or what you expect which you claim does not happen. Let's insert an extra line above the
connect()
and change the lambda slot slightly so it reads:qDebug() << index << index_sub; connect(subMenu[index], &QMenu::triggered, this, [=]() { qDebug() << index << index_sub; } );
-
When these two lines of code execute, i.e. the
connect()
statement is executed, you will see the values forindex
&index_sub
at that instant. I don't know what they are, but let's say1
&3
. -
When whatever
QMenu
item was atsubMenu[index]
at the time this was executed is latertriggered
, you will see the same values forindex
&index_sub
as they were when theconnect()
line was previously executed. In the above case they would be1
and3
again.
Of course, if you are executing the
connect()
inside a loop(s) whereindex
and/orindex_sub
are changing you will end up with more than one lambda slot attached. You will (potentially) have more than one slot attached to a givenQMenu
item and/or slot(s) attached to more than oneQMenu
item.- Each time
index
changes you will be attaching to a differentsubMenu[index]
item. - Each time
index_sub
changes you will be attaching to the samesubMenu[index]
item but with a different value for theqDebug() << index_sub
in the lambda slot.
-
-
@AnneRanch said in help to find "connect" error:
both indexes - index and index-sub are STATIC, they are NOT created when "click on submenu" is activated.
Well I'm not surprised you're lambda does not snapshot shot
index
orindex_sub
during the moment of the connect call but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted.this will snapshot index and index_sub:
connect(subMenu[index], &QMenu::triggered, this , [this, i = index, i_s = index_sub]()->void{ this->processMenu(i,i_s) ;});
-
@J-Hilk said in help to find "connect" error:
Well I'm not surprised you're lambda does not snapshot shot index or index_sub during the moment of the connect call but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted.
I do not understand why you have written this. The OP's lambda, and my post, use
[=]
as the capture. That will "snapshot" (as you call it) the values at the instant theconnect()
statement is executed, as I wrote. So why are you sayingbut it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted
I do not see your
[this, i = index, i_s = index_sub]
will behave any differently from[=]
in this case? Do you really want me to go try, are you saying I have misunderstood C++ lambdas/``[=]` ??which it will not (as
&
was not used)?WAIT
Are you sayingindex
andindex_sub
are class member variables, not local ones wherever the code is?? I thought they were locals, I think they are, I think the OP is sjowing us some function where it has// results int submenu_index = -1; int mainmenu_index= -1;
at the top, but it's hard to tell....
-
QApplication app(argc,argv); static int index{0}, index_sub{0}; auto lambda1 = [=]()->void{qDebug() << index << index_sub;}; auto lambda2 = [=, i = index, i_s = index_sub]()->void{qDebug() << i << i_s;}; lambda1(); lambda2(); index = 100; index_sub = 100; lambda1(); lambda2();
->
0 0 0 0 100 100 0 0
Are you saying index and index_sub are class member variables, not local ones wherever the code is??
that's what the 2nd post is saying, at least what I understand it's saying.
-
@J-Hilk
Nope! Please read, you have an "edge" case of some kind in your code.To do your
static int index{0}, index_sub{0};
I had to change my default C++ compilation fromc++11
toc++14
. There may be a clue here (actually I'm not sure this is relevant). Withc++11
and your code I get/home/jon/QtTests/lambdacopy/main.cpp:11: warning: lambda capture initializers only available with ‘-std=c++14’ or ‘-std=gnu++14’ ../lambdacopy/main.cpp: In function ‘int main(int, char**)’: ../lambdacopy/main.cpp:11:24: warning: lambda capture initializers only available with ‘-std=c++14’ or ‘-std=gnu++14’ 11 | auto lambda2 = [=, i = index, i_s = index_sub]()->void{qDebug() << i << i_s;}; | ^
Please change your code to remove the
static
, so it reads/*static*/ int index{0}, index_sub{0};
Now the output reads
0 0 0 0 0 0 0 0
which is exactly what I expected.
The behaviour of
[=]
seems to be different on local variables which arestatic
? But we have no evidence those would bestatic
in OP's code, and I would never have tried withstatic
.....EDIT
Ohhhhh, now I see the OP'sboth indexes - index and index-sub are STATIC, they are NOT created when "click on subnenu" is activated.
@AnneRanch
Now that I see this. (You had never posted the declarations of these variables, and nostatic
keyword appears in any of your code, which is why we really need complete examples to help.) For your purposes you must NOT havestatic index, index_sub
. This is why they are not delivering the values they had at the time of theconnect()
but rather the values they have from the last iteration. Or follow @J.Hilk's[this, i = index, i_s = index_sub]()->void{ this->processMenu(i,i_s) ; }
to copy their current values into
i
&i_s
(or whatever) so that the lambda gets a copy of their current value. -
@JonB what exactly is your point of content with my example/solution?
With c++11 and your code I get
of course, capture initalizeres
[i = index]
are a c++17 feature independently from index being static or notPlease change your code to remove the static, so it reads
Now the output readsOf course, I would expect nothing else. I'm pretty sure one of either the capture initialisers or the capture by copy of the local variables are skipped by the compiler.
The behaviour of [=] seems to be different on local variables which are static? But we have no evidence those would be static in OP's code, and I would never have tried with static.....
well in this case, static and/or member variables. I was too lazy to write a whole class for this example. And like I quoted, her 2nd comment clearly states
static
member variables of index and index_sub -
@J-Hilk said in help to find "connect" error:
And like I quoted, her 2nd comment clearly states
static
member variables of index and index_subEvidently easier to spot that by some (you) than by others (me)! There is a lot to wade through (unformatted), and it always helps to show the relevant code...! :)
Anyway, hopefully the OP has a couple of alternative ways to modify the code now to get the behaviour desired.