Re: QThread run() makes MainWindow get stuck
Quote:
Originally Posted by
kerim
but isnt it bad that one thread manipulates gui belonging to other thread ?
QObjects "belong" to the thread that creates them until you push them to a different thread using moveToThread(). So if you create the object in the worker thread and initialize it with data there and then push the result to the main thread, it is likely it will work.
Quote:
how would/could code look (just small example) that uses local QTextDocument which is transfered and used from gui after worker-thread has finished??
Sorry, I'm done with doing one's work for him. You have been given all needed details. Dig in and have fun.
Re: QThread run() makes MainWindow get stuck
@wysota:
thnx alot man, the first part of ur last post was the info i needed and in regard to the second part: i didnt wanted you to come up with a hundrets-of-lines example :), the point that made me suspicous about manipulating gui beyond threads was just made clear by the first part of ur comment ;), thnx alot.
i ll try that.
cheers.
Added after 59 minutes:
:confused: i am still missing something i think:
my current relevant code parts in regard to the worker-thread declaration are:
Code:
// //////////////////////////////////////////
// //////////////////////////////////////////
{
Q_OBJECT
// //////////////////////////////
public:
// //////////////////////////////
~FileLoader();
signals:
protected:
void run();
// //////////////////////////////
private:
// //////////////////////////////
CLogFile logfile;
};
(everytime before workerthread manipulates QTextDocument member its calling moveToThread(this) within its running method):
Code:
// //////////////////////////////////
void FileLoader::run()
// //////////////////////////////////
{
...
string content;
doc->moveToThread(this);
// manipulate doc here ...
emit WorkDone(doc);
}
the workerthread emits once the WorkDone(QTextDocument *) signal when work is done wich is caught by the main-threads slot
Code:
{
ui->textEdit->setDocument(doc);
}
the last line here (setDocument) fails and throws a segmentation fault which crashes my application, what am i doing wrong now !?!?
here is the trace:
http://img132.imageshack.us/f/11178576.gif/
Re: QThread run() makes MainWindow get stuck
It won't work this way. You are trying to "pull" an object into your own thread instead of "pushing" it out of the thread it lives in. This probably fails and you get a segfault when trying to access an object that doesn't belong to you.
Re: QThread run() makes MainWindow get stuck
Quote:
Originally Posted by
wysota
It won't work this way. You are trying to "pull" an object into your own thread instead of "pushing" it out of the thread it lives in. This probably fails and you get a segfault when trying to access an object that doesn't belong to you.
ok, so whats the difference between "pushing" and "pulling" the object ?
i just tried it with the moveToThread(..) thing u mentioned.
if this is some general issue problem or something about to be read from some documentation, i appreciate read links too.
Re: QThread run() makes MainWindow get stuck
You "pull" things that are far away towards you, you "push" things that are close to you away. So in our situation it means you can move the object from your thread to some other thread (push) but not from another thread to your own thread (pull) - you can't "steal" an object from another thread. It's all in QObject::moveToThread() docs - see the warning at the end.
Re: QThread run() makes MainWindow get stuck
Quote:
Originally Posted by
wysota
You "pull" things that are far away towards you, you "push" things that are close to you away. So in our situation it means you can move the object from your thread to some other thread (push) but not from another thread to your own thread (pull) - you can't "steal" an object from another thread. It's all in
QObject::moveToThread() docs - see the warning at the end.
well it sais:
"Warning: This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread."
i assume with saying current thread the thread calling moveToThread is ment, but what is thread-affinity in this context (does that mean the adress space of the thread)!?!?
i really dont have a clue how to achive a none crashing example:
- where should the QTextDocument be created (heap or stack ?)
- how do i pass the QTextDocument to the main-thread to set it for the QTextEdit?
:(
Re: QThread run() makes MainWindow get stuck
I really wouldn't like to repeat what is already Qt docs. Read Threads and QObjects