Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: QThread run() makes MainWindow get stuck

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread run() makes MainWindow get stuck

    Quote Originally Posted by kerim View Post
    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.

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #22
    Join Date
    Mar 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default 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:


    i am still missing something i think:

    my current relevant code parts in regard to the worker-thread declaration are:
    Qt Code:
    1. // //////////////////////////////////////////
    2. class FileLoader : public QThread
    3. // //////////////////////////////////////////
    4. {
    5. Q_OBJECT
    6.  
    7. // //////////////////////////////
    8. public:
    9. // //////////////////////////////
    10. FileLoader(QObject *parent);
    11. ~FileLoader();
    12.  
    13. signals:
    14. void WorkDone(QTextDocument *);
    15.  
    16. protected:
    17. void run();
    18.  
    19. // //////////////////////////////
    20. private:
    21. // //////////////////////////////
    22. CLogFile logfile;
    23. };
    To copy to clipboard, switch view to plain text mode 

    (everytime before workerthread manipulates QTextDocument member its calling moveToThread(this) within its running method):

    Qt Code:
    1. // //////////////////////////////////
    2. void FileLoader::run()
    3. // //////////////////////////////////
    4. {
    5. ...
    6. string content;
    7.  
    8. doc->moveToThread(this);
    9. QTextCursor cursor(doc);
    10.  
    11. // manipulate doc here ...
    12.  
    13. emit WorkDone(doc);
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 


    the workerthread emits once the WorkDone(QTextDocument *) signal when work is done wich is caught by the main-threads slot
    Qt Code:
    1. void PutDoc(QTextDocument *doc)
    2. {
    3. doc->moveToThread(QApplication::instance()->thread());
    4. ui->textEdit->setDocument(doc);
    5. }
    To copy to clipboard, switch view to plain text mode 
    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/
    Last edited by kerim; 27th March 2011 at 13:57.

  3. #23
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #24
    Join Date
    Mar 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: QThread run() makes MainWindow get stuck

    Quote Originally Posted by wysota View Post
    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.

  5. #25
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    kerim (27th March 2011)

  7. #26
    Join Date
    Mar 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Unhappy Re: QThread run() makes MainWindow get stuck

    Quote Originally Posted by wysota View Post
    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?

  8. #27
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread run() makes MainWindow get stuck

    I really wouldn't like to repeat what is already Qt docs. Read Threads and QObjects

Similar Threads

  1. Changing MainWindow UI from another QThread.
    By EdgeLuxe in forum Newbie
    Replies: 1
    Last Post: 4th September 2010, 16:35
  2. QPushButton gets stuck
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2010, 00:22
  3. Control MainWindow from QThread
    By lixo1 in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2009, 11:33
  4. Help plz! I’m stuck with these delegates
    By codeaddict in forum Qt Programming
    Replies: 7
    Last Post: 19th August 2008, 22:33
  5. comboboxes get stuck
    By illuzioner in forum Qt Programming
    Replies: 2
    Last Post: 30th March 2006, 02:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.