PDA

View Full Version : Thread design advice needed



jonks
22nd October 2009, 02:41
My question is inside the run function below ;)


class TestObject : public QThread
{
Q_OBJECT
private:
QNetworkReply *m_reply;
QUrl m_url;

public slots:
void gotNetworkHeader()
{
// do something
}

public:
TestObject(QUrl url) : m_url(url)
{
}

void run()
{
QNetworkAccessManager nam;
m_reply = nam.head(QNetworkRequest(m_url));
connect(m_reply, SIGNAL(finished()), this, SLOT(gotNetworkHeader()));

//
// what do I do here to stop the thread from
// exiting until gotNetworkHeader has finished
// execution?
//

// I've tried exec(), but this seems to block QNetworkAccessManager

}
};

jonks
22nd October 2009, 06:24
I just stumbled across this, which has some examples which are completely different to my design, but do exactly what I want.

http://doc.trolltech.com/qq/qq27-responsive-guis.html

wagmare
22nd October 2009, 19:13
simple
assign a private bool function
private:
bool stop;

in your constructor
stopped = false; //declare it
also use connect here itself in constructor

in run () function

while(stop)
{
//busy line ...
}

and in private slot


gotNetworkHeader()
{
stop = true;
}

wysota
22nd October 2009, 22:32
Actually exec() would do the job. Just remember the QThread object lives in the main thread and not in the thread it controls.