Solved Function with Signals & Slots
-
@gabor53
It's not the lambda.imagePath = findimage(QString);
This doesn't make sense neither to the compiler nor to me, so you should fix it. What argument are you trying to use
findimage
with? Currently it has none, and instead of an argument a type name is provided.Kind regards.
-
@kshegunov
ahh good spotted. thats the void = QString :)
I completely missed that .) -
@mrjj
I just read the compiler errors, it says it right there:expected primary-expression before ')' token
;)
-
@kshegunov
yeah it's a meaningful and good compiler error :) -
Heheh, of course that's right. I gotta get back to programming again, I been installing , upgrading and reading too much...
** it doesn't matter where fileName (or m_fileName) is declared, after the connect(...) call it will not be set yet because the findimage(bool) slot was not yet called. It will be called when the user clicks on the button - and you never know when the user will click the button. ** -
I changed the following line:
connect(Image_Button, &QPushButton::clicked, [this]() { imagePath = findimage( QString);}); to
connect(Image_Button, &QPushButton::clicked, [this]() { imagePath = findimage( fileName);});
Now I get the following error message: C:\Programming\backup\Folkfriends\additem.cpp:195: error: no match for 'operator=' (operand types are 'QString' and 'void') connect(Image_Button, &QPushButton::clicked, [this]() { imagePath = findimage( fileName);}); What else to change? Thank you.
-
Hi
in the first post you show it as
findimage(bool)so that would be callable as
imagePath = findimage( true);So what you give it as parameter, depends on how u declare it.
(in the .h file) -
@mrjj
I changed it from findimage(bool) to findimage(fileName) because I need it to return the fileName.
In the .h file it is defined as void findimage(QString fileName);
I still have the same error message. Thank you. -
@gabor53 said:
hi
I would kinda expect it to be declared as
void findimage();
as you
return(fileName);
in the function and return filename that way.
so why you need a QString as parameter too ? -
@mrjj
I changed it as you recommended:
in the .h file: void findimage();connect(Image_Button, &QPushButton::clicked, [this]() { imagePath = findimage();}); the function : void Additem::findimage(); It still gives the same exact error message. Thnks.
-
@gabor53
void means "nothing", so you're trying to save that "nothing" into a variable, how should that happen? The compiler doesn't know, I don't and I'm pretty sure no one does. If you want to assign a value returned from a function to a variable, well then, by all means, make the function return the proper type and value. Meaning, if you want the function to return aQString
then you declare it as such:class MyClass { QString myFunction(); //< MyClass::myFunction is going to return a value of type QString. } QString MyClass::myFunction() { return QString("Some string"); //< The function returns the value and it is of type QString }
-
sorry my bad
void findimage();should have been
QString findimage();
I think i should let @kshegunov handle this one as I keep missing the errors :)
-
@mrjj
I changed the .h file declaration to QString findimage();.
Now it works without error, but I still can't capture the findimage returned value (fileName) in the line after connect.
code
The function works because the qDebug in the last line returns the correct value.
How can I return the value before the add layout section so I would be able to display the image next to the button? (That was the original issue I had).Thank you.
-
@mrjj
As I noted before, the errors are swiftly and cleanly reported by the compiler:C:\Programming\backup\Folkfriends\additem.cpp:195: error: no match for 'operator=' (operand types are 'QString' and 'void')
connect(Image_Button, &QPushButton::clicked, this { imagePath = findimage( fileName);});So the compiler can't make heads or tails of what to do with the arguments given to the assignment operator. :)
-
http://forum.qt.io/topic/64635/function-with-signals-slots/12
@jsulm already gave you what the problem with this construct is. You're trying to predict the future. You want at some point when the user clicks a button to take an image, and that's fine. The erroneous part is when you want to go back in the past where the connect was made and do something with that image, it's simply not possible. If you want to operate on the value returned from your function, then you should put the code that uses it inside the lambda function, so you wouldn't need to jump through time-travel hoops to get the string.
QObject::connect
simply says that some function should be called when a signal is emitted, it doesn't guarantee you in any way when or if that shall happen.Kind regards.
-
Thank you all. It worked.