PDA

View Full Version : [SOLVED] share data between caller and slot



whites11
24th April 2010, 11:24
hi.
my need is to pass data between a method that calls an async request and the slot that is connected to the relative signal.

an example to make this clear:



void foo()
{
QString str1 = "str1";

connect(requestManager, SIGNAL(requestFinished()), this SLOT(slot2()));

this->requestManager->startRequest(); //emits requestFinished()
}

void slot2()
{
//here i need str1 from foo
}


requestManager is an unmodifiable class.
one way is to save str1 as a member of the class, but i'm wondering if there is a better way to do this.


thanks in advance for any answer

borisbn
24th April 2010, 12:46
your requestManager is a QObject inherited, that's why you can add a property to it and retrieve it within ANY slot (not only in your class)


const char * MY_PROPERTY_NAME = "property name";
void foo()
{
QString str1 = "str1";

requestManager->setProperty( MY_PROPERTY_NAME, str1 );

connect(requestManager, SIGNAL(requestFinished()), this SLOT(slot2()));

this->requestManager->startRequest(); //emits requestFinished()
}

void slot2()
{
//here i need str1 from foo
QString str1 = sender()->property( MY_PROPERTY_NAME ).toString();
}

whites11
24th April 2010, 13:34
thanks man, that's exactly what i was searching for!