Solved Function with Signals & Slots
-
Does ur compiler support lambdas?
Try with empty { }connect(Image_Button, &QPushButton::clicked, [this]() { })
you need Qt 5.5 and newer compiler.
-
@mrjj
How can I find out if it supports lambdas? -
@gabor53
well it can then compile the line.
what compiler is it ?
VS or mingw?
and what version? -
@mrjj
I have mingw 4.9.2 32 bit -
@gabor53
ahh, u might need
CONFIG+=c++11
in your .pro file for it to use c++ 11 :) -
@mrjj
I have that too, so I guess it supports lambdas. -
@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. :)