PDA

View Full Version : Communication between Threads



Wan-Hi
19th March 2006, 01:24
hello.

my little app has two threads:

Thread 1: the regular main thread, where GUI rendering and short term computation is done.
Thread 2: a worker thread for time consuming actions (file copy session).

i want the worker thread to notify a custom made progress dialog, consisting of two QProgressBars and a QTextEdit, when to display new infos. can this be done by posting custom QEvents? i'm not sure because the signal / slot mechanism also doesn't work between threads.

wan-hi

jacek
19th March 2006, 02:31
I'm not sure because because the signal / slot mechanism also doesn't work between threads.
It does work in Qt4, but you must use queued connections.

Wan-Hi
19th March 2006, 05:14
i did use queued connections, trying to notify the main thread by the worker thread. but i get errors, although everything seems to be right.



CustomProgressDialog::CustomProgressDialog(Task * task, QWidget * parent) : QDialog(parent) {
...
connect(task, SIGNAL(stepCompleted(QString)), this, SLOT(stepCompleted(QString)), Qt::QueuedConnection);
...
}

void CustomProgressDialog::stepCompleted(QString msg) {
QTextCursor cursor(doc);
cursor.movePosition(QTextCursor::End);
taskEdit->setTextCursor(cursor);
taskEdit->insertPlainText(description);
}


during debugging i always get an assert error, saying that new widgets may only be created in the UI thread (main thread). this happens as soon as the slot function is executed. it's irritating because per definition all slots of QueuedConnections are executed in the thread of the target's thread, which in this case IS the UI thread. :confused:

jacek
19th March 2006, 14:09
How do you create that CustomProgressDialog instance? What is that Task class? Does it inherit QThread?

Wan-Hi
19th March 2006, 15:40
yes, Task inherits QThread. the CustomProgressDialog is created on the stack within the main UI thread, inside a MainWindow's function to be more exact. strange...

naresh
19th March 2006, 16:36
To post data from thread to gui i used custom QEvent... here is example:


class printE: public QEvent
{
public:
printE(QString str, Type type=(QEvent::Type)1001); // here should be int between 1000 and 65535
Type type();
QString data();

private:
Type t;
QString string;
};

printE::printE(QString str, Type type):QEvent(type)
{
t = type;
string = str;
}

QEvent::Type printE::type()
{
return t;
}

QString printE::data()
{
return string;
}

Of course you can add much more data into this event... In your customEvent(QEvent *e) you should place something like that to recieve data from your custom event


if(e->type() == (QEvent::Type)1001)
{
QString str=((printE*)e)->data();
// do something with str
}

I hope it'll help somehow

jacek
19th March 2006, 16:53
yes, Task inherits QThread. the CustomProgressDialog is created on the stack within the main UI thread, inside a MainWindow's function to be more exact. strange...
Indeed, something strange is going on.

Wan-Hi
19th March 2006, 21:59
thanks for the help. i was thinking about using custom QEvents, but i managed to make queued connections work. it turned out that a QMessageBox::information got left over in the worker thread which i forgot to remove.

alexemil5
23rd January 2013, 13:46
I was also felt help in code.. So now it is resolved. It is good to see this info about to communication in threads..

anda_skoa
23rd January 2013, 14:13
thanks for the help. i was thinking about using custom QEvents, but i managed to make queued connections work.

You only need custom events when doing that kind of communication in Qt3 which didn't have Qt::QueuedConnection yet.

Cheers,
_

wysota
23rd January 2013, 14:23
Kevin, it's a seven year old zombie thread :D

anda_skoa
24th January 2013, 13:52
Oh boy, didn't check :)

Anyway, my reply's content still stands :D

Cheers,
_