Solved Function with Signals & Slots
-
right , I should have taken the tempFileName= out , it isn't even needed ...Hmm , looking over a code a bit closer
QString Additem::findimage(bool)
return(fileName);
connect (Image_Button,SIGNAL(clicked(bool)),this,SLOT(findimage(bool)));
It looks like findimage returns a filename , not a SLOT , so this code would not work. I guess the only way that it could potentially work is if it had a list of slotnames in the QString and it returned them to connect. ( I am really just getting to know the QT environment, as well) . I must say I do like the concept of signals and slots vs events & triggers.
-
Now in his original code for the slot he is using (findimage(bool)) , in the function code he can sure fileName is declared either public or private in the class's header ( m_fileName for example ) so that all the Additem's other class functions will have access to it. Get and Set accessor functions are commonly used with class variables (for many reasons I have discovered) .
QString Additem::findimage(bool)
m_fileName = fileDialog->getOpenFileName(this,
tr("Finding Friend's Image"),sPath, tr("Image Files (*.png *.jpg *.bmp)"));return m_fileName;
-
The thing is:
connect (Image_Button,SIGNAL(clicked(bool)),this,SLOT(findimage(bool))); qDebug() <<"The image path is (in main): " << fileName;
- 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.
-
Thank you all. Now I understand what's wrong. After the user clicks Image_Button, I want to display the image in a grid layout in which the Image_Button is. Is there a way I can go back to the grid layout AFTER the user clicked Image_Button and insert the choosen image next to the button?
-
Hi if u use @JordanHarris smart version,
you could do something like
connect(imageButton, &QPushButton::clicked, this {
imagePath = findImage();
QPixmap pic(imagePath);
ui->SomeQLabel->setPixmap(pic);
});you must insert the "SomeQLabel" into the layout. (just a normal Qlabel)
-
@mrjj ,
I implementedconnect(Image_Button, &QPushButton::clicked, [this]() { imagePath = findimage(); })
and I got 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(); });
^
I'm wondering what am I missing. -
well basically it says you are doing
void = QString
so check that findimage returns Qstring and that
imagePath is also declared Qstring -
-
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.