Results 1 to 2 of 2

Thread: Thread Help

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Thread Help

    Hi,

    Im having some problems with cross thread communication, basically i get an assert error:

    "ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 72fc768. Receiver 'ProDialog' (of type 'progressDlg') was created in thread 50d4058", file kernel\qcoreapplication.cpp, line 296"
    Im using signals/slots to send data from the new thread to the ProDialog instance, and im not sure why it doesn't like it :confused the only shared object is the container instance, i havent bothered locking/unlocking this as I know its not in use while the thread is executing, hopefully thats good enough.

    Heres some code extracts:

    Main window code:
    Qt Code:
    1. // Create progress dialog
    2. pro = new progressDlg();
    3. pro->show();
    4.  
    5. // Spawn a new worker thread
    6. enc = new TEnc;
    7.  
    8. // Connect signals/slots
    9. connect(enc, SIGNAL(StartingFile(QString)), pro, SLOT(setProgressLabel(const QString&)), Qt::DirectConnection);
    10. connect(enc, SIGNAL(AddFilesDone(bool)), this, SLOT(AddFilesComplete(bool)));
    11. connect(enc, SIGNAL(Progress(int, int)), pro, SLOT(setProgress(int, int)), Qt::DirectConnection);
    12.  
    13. enc->setParams(container, files);
    14. enc->start(); // start the thread
    To copy to clipboard, switch view to plain text mode 


    The TEnc object (QThread subclass)
    Qt Code:
    1. void TEnc::run()
    2. {
    3. emit Progress(files.size(), 0);
    4.  
    5. // do the work
    6. for(int i = 0; i < files.size(); i++)
    7. {
    8. bits = files.at(i).split("/"); // extract file name from path
    9. if(bits.size() == 0)
    10. bits = files.at(i).split("\\");
    11.  
    12. if(bits.size() != 0)
    13. emit StartingFile("[" + QString::number(i+1) + "/" + QString::number(files.size()) + "] " + bits.at(bits.size() - 1)); // send signal out that we are starting
    14.  
    15. if(!container->AddItem(files.at(i).toAscii(), NULL))
    16. {
    17. emit AddFilesDone(false); // fail with error
    18. return;
    19. }
    20.  
    21. emit Progress(files.size(), i + 1); // update progress
    22. }
    23.  
    24. // all done signal
    25. emit AddFilesDone(true); // singal -> all files done
    26. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas what silly mistake ive made this time

    Thanks,

    Jack

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread Help

    Removed the Qt::DirectConnection parameter. You don't want to invoke direct calls from the worker thread to the GUI thread.
    J-P Nurmi

Similar Threads

  1. GUI and non-GUI thread problem
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 21:25
  2. QT + OpenGL + Thread => aaaahhhhh !
    By anthibug in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2008, 13:36
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

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.