PDA

View Full Version : Threads in GUI application



blackliteon
11th April 2006, 18:11
Hi all!
Just want to write to the textarea of main application with threads.
Implemeth Thread class:

#include <QTextEdit>
#include <QThread>

class Worker : public QThread
{
Q_OBJECT

public:
void run();
void connectTextOutput(QTextEdit *te);
Worker();
~Worker();

private:
QTextEdit *text;
QString q;
};

and realization:

void Worker::run()
{
text->insertPlainText(q + "AND time is come true\n");
q += "22";
exec();
}

void Worker::connectTextOutput(QTextEdit *te)
{
text = te;

}

And in Main application constructor:

w = new Worker();
w->connectTextOutput(ui.textLog);
w->start();

So...

A get one string writed to text area.
Threat not workedd..
Whats wrong ?

jacek
11th April 2006, 18:12
You can't access widgets from a non-GUI thread. All you can do is to send them a signal through a queued connection.

AlexKiriukha
12th April 2006, 07:47
I don't know about Windows and Mac, but when I've tried to paint (via QPainter) from non-gui thread, I had a lot of X11 errors. Maybe it's because my x.org API is not reentrant.

blackliteon
12th April 2006, 08:52
Thanks!
Try this today!

blackliteon
12th April 2006, 11:01
You can't access widgets from a non-GUI thread. All you can do is to send them a signal through a queued connection.
What I need to do to make my Thred class - GUI class ?

jacek
12th April 2006, 13:01
What I need to do to make my Thred class - GUI class ?
You can't do this. There can be only one GUI thread (i.e. the one which created the QApplication instance).

Chicken Blood Machine
12th April 2006, 17:08
I don't know about Windows and Mac, but when I've tried to paint (via QPainter) from non-gui thread, I had a lot of X11 errors. Maybe it's because my x.org API is not reentrant.
Did you read the documentation on QThread?

AlexKiriukha
14th April 2006, 09:48
Did you read the documentation on QThread?

Sure. I know that application can have only one GUI thread but experiments (even if they are wrong) is interesting thing :-)