PDA

View Full Version : Button Return



lorin
8th January 2011, 19:00
How can i return any value when i will click to button. It is possible to use QObject::connect(&get, SIGNAL(clicked()), &give, SLOT(return value)?




int main(int argc, char *argv[]) {
int a = 10;

QApplication app(int argc, char *argv[]);

QWidget wid1;
wid1.resize(100, 100);

QWidget wid2;
wid2.resize(100,100);

QPushButton but1("Text", &wid1);
but1.geometry(0,0,100,100);
QObject::connect(&but1, SIGNAL(clicked()), &wid1, SLOT(>>return value<<));

QPushButton but2("Text", &wid2);
but2.geometry(0.0.100.100);
QObject::connect(&but2, SIGNAL(clicked()), &wid1, SLOT(>>return value<<));
}


(ignore the code errors... i want only know, if i can použÃ*t QObject::connect(&but2, SIGNAL(clicked()), &wid1, SLOT(return a = 50;));

Zlatomir
8th January 2011, 19:26
I didn't quite understand what are you trying to achieve...

So here are some stuff that might (depending on what you want to do) help you:
You can encapsulate that int a as a member in the same class with your button and the slot, and then connect the clicked() signal with some slot_name() and in that slot:
1) you can call the other slot_with_int(a) //slots can be called directly just like member functions (because they basically are member functions)
or
2) code another signal that has a int parameter and emit the my_signal(a) //of-course in this case you need another slot that will do the work and a connection to it

Added after 12 minutes:


QObject::connect(&but2, SIGNAL(clicked()), &wid1, SLOT(return a = 50;));
Yes you can do that, but the connect statement should look like this:

connect(&but2, SIGNAL(clicked()), &wid1, SLOT(slot_name() ));

And slot_name can have a default value:

void slot_name(int x = 50)

Another advice is to use the heap to allocate QWidgets.

lorin
8th January 2011, 19:39
Thanks you a lot!