PDA

View Full Version : QThread and method with pointer argument



kaszewczyk
9th November 2009, 08:29
Hello, my problem is:
for example i have a class


class FooClass{
public:
FooClass();
~FooClass();

private:
QTextEdit* text;
};
FooClass::FooClass(){
text = new QTextEdit;
}

and now i want to make thread where i can put something in my FooClass::text fild


class FooThread : public QThread{
public:
void setTextEdit(QTextEdit* __p);

protected:
void run()

private:
QTextEdirt* textPointer;
};
void FooThread::setTextEdit(QTextEdit* __p){
this->textPointer = __p
}

void FooThread::run(){
this->textPointer->insertPlainText("Hello from QThred");
}

my aim is to enter ""Hello from QThred" string into FooClass TextEdit


FooClass::FooClass(){
text = new QTextEdit;
FooThread *thread = new FooThread;
thread->setTextEdit(this->text);
thread->start();
}

but when i do it like this i get runtime error :/

wysota
9th November 2009, 09:17
You can't access widgets from within worker threads. Use signals instead to communicate with the main thread and change the contents of the text edit from within there.

sadjoker
11th November 2009, 13:51
Emit a signal in the thread`s run() maybe with some QString argument (the text which you want) and catch the signal with a proprietary slot in the main thread which has the same QString argument to get the text and change the text of the widget in that slot.