Results 1 to 9 of 9

Thread: MainThread and QThread

  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default MainThread and QThread

    Hi,
    I have a MainWindow with a QTabWidget and a QPlainTextEdit. If I click on a button an Object is created and a Thread. The Object is moved to the thread and some signals and slots get connected:
    Qt Code:
    1. BaseExport* creator = new BaseExport(NULL);
    2. MyThread* mthread = new MyThread(NULL);
    3.  
    4. creator->moveToThread(mthread);
    5.  
    6. connect(creator,SIGNAL(new_status(QString)),this,SLOT(status_received(QString)),Qt::QueuedConnection);
    7. connect(creator,SIGNAL(finished()),ui->progressBar,SLOT(hide()),Qt::QueuedConnection);
    8. connect(creator,SIGNAL(finished()),creator,SLOT(deleteLater()),Qt::QueuedConnection);
    9. connect(creator,SIGNAL(error(QString)),this,SLOT(error_received(QString)),Qt::QueuedConnection);
    10. connect(creator,SIGNAL(more_than_one_result(QList<QList<SetField *> *> *)),this,SLOT(more_than_one_result_slot(QList<QList<MatchingSetField*>*>*)),Qt::QueuedConnection);
    11. connect(this,SIGNAL(decided(QList<SetField*>*)),creator,SLOT(decided_slot(QList<SetField*>*)),Qt::QueuedConnection);
    12. creator->connect(mthread,SIGNAL(started()),SLOT(create()));
    13. connect(creator,SIGNAL(finished()),mthread,SLOT(quit()));
    14. connect(mthread,SIGNAL(finished()),mthread,SLOT(deleteLater()));
    15.  
    16. mthread->start();
    To copy to clipboard, switch view to plain text mode 

    Everything works fine. The create Slot of my Object starts and do some work. The signals (new_status, error) are received and the guithread shows the status and the errors.
    It works to the point where the creator object in the thread need some information from the user. The object emits the signal more_than_one_result(). The receiving slot opens a QDialog with a table, which shows the results.
    After choosing a result, the Mainthread emits the signal decided and sends a QList back to the thread.

    The thread waits for the response in an QEventLoop waitloop;
    In the slot decided_slot the waitloop ends with the call waitloop.quit(). And there sometimes occurs the error:
    Qt Code:
    1. QPixmap: It is not safe to use pixmaps outside the GUI thread
    2. QPixmap: It is not safe to use pixmaps outside the GUI thread
    To copy to clipboard, switch view to plain text mode 

    and the app crashes.
    What's wrong? Maybe somebody can give me a hint. Thank u
    Last edited by Qiieha; 21st May 2012 at 17:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MainThread and QThread

    It seems that you are accessing GUI elements into the thread.
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    Qiieha (21st May 2012)

  4. #3
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainThread and QThread

    Thank u for your help.
    No I don't access gui elements. In Guithread a method sorts out the QList after that the signal decided(QList<Object*>*) is emitted.

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MainThread and QThread

    "No I don't access gui elements. In Guithread a method sorts out the QList after that the signal decided(QList<Object*>*) is emitted."

    Qt disagrees with you


    It might help if you showed all code related to qpixmap. Including where they are passed around through QObject/QWidget pointers.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #5
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainThread and QThread

    I don't use qpixmap in the application.

    this is the method, where signal is emitted:
    Qt Code:
    1. void IntWidget::more_than_one_result_slot(QList<QList<SetField *> *> *result_list)
    2. {
    3. qDebug() << "IntWidget::more_than_one_result_slot(QList<QList<SetField *> *> *result_list)";
    4. qDebug() << this->thread();
    5. DecideDlg dlg(this,result_list);
    6. dlg.exec();
    7. QList<MatchingSetField*>* chosen_list = dlg.getChosenList();
    8. //emit the signal. the slot is in Thread
    9. emit decided(chosen_list);
    10. }
    To copy to clipboard, switch view to plain text mode 
    this is the slot
    Qt Code:
    1. void BaseExport::decided_slot(QList<SetField *> *decide_list)
    2. {
    3. qDebug() << "BaseExport::decided_slot(QList<SetField *> *decide_list)";
    4. for(int i = 0; i < decide_list->size(); i++){
    5. SetField* decided_setfield = decide_list->value(i);
    6. Set* current_set = sets->value(decided_setfield->getMsId());
    7. SetField* origin_setfield = current_set->getSetFields()->value(decided_setfield->getId());
    8. origin_setfield->setResult_Value(decided_setfield->getResult_Value());
    9. delete decided_setfield;
    10. }
    11. delete decide_list;
    12. waitloop.quit();
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainThread and QThread

    The behaviour above occurs at a Dual Core Processor. On a single Core Processor the slot of the Object in the Thread is never executed.

    Nobody has an Idea?

  8. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MainThread and QThread

    what is the class def for SetField?


    your single core problem is probably due to your 'wait loop' / misuse of event loops / whatever other blocking tactics you use.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #8
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainThread and QThread

    Qt Code:
    1. class SetField
    2. {
    3. private:
    4. int id;
    5. int reg_id;
    6. int foo_id;
    7. QString cast;
    8. int field;
    9.  
    10. public:
    11. SetField(int id, int reg_id, int foo_id, QString cast, int field);
    12. ~SetField();
    13.  
    14. //setter
    15. //getter
    16. };
    To copy to clipboard, switch view to plain text mode 

    Ok, then it is probably a misuse of QEventLoop. The Thread should wait for the response. What technic I should use?


    Added after 41 minutes:


    I solved the Problem!
    It was a misuse of QEventLoop. BlockingQueuedConnection solved the Problem. QEventLoop is unnecessary in this case....
    Last edited by Qiieha; 22nd May 2012 at 11:42.

  10. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MainThread and QThread

    "Ok, then it is probably a misuse of QEventLoop. The Thread should wait for the response. What technic I should use?"
    signal/slot queued connection
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. The following user says thank you to amleto for this useful post:

    Qiieha (22nd May 2012)

Similar Threads

  1. Calling QThread.wait() from mainthread
    By P@u1 in forum Qt Programming
    Replies: 3
    Last Post: 1st June 2011, 15:41
  2. Replies: 0
    Last Post: 26th February 2010, 19:01
  3. Replies: 4
    Last Post: 26th June 2008, 18:41
  4. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  5. QThread
    By TheKedge in forum Qt Programming
    Replies: 8
    Last Post: 25th August 2006, 10:29

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.