Results 1 to 5 of 5

Thread: Threading Issue

  1. #1
    Join Date
    May 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Threading Issue

    Hii..

    I encountered an error when I tried to execute following line of code.

    statusbar()->message(strError,15000);

    The error got is "ASSERT failure in QCoreApplication::sentEvent:"Cannot sent events to objects owned by a different thread".

    This code works fine with Qt3 and the problem is only with Qt4.The code is ported from Qt3.3.8 to Qt 4.3.0.

    I will heartly appreciate any suggestions.

    Regards
    Noufal

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threading Issue

    So, you call that from a worker thread, right?
    Well, in Qt4 this does not work anymore. No widget can be modified by any thread other than the GUI thread.

    Solution:
    Add a signal to the worker thread, with 2 parameter.
    Add a slot in the GUI class that owns/has access to the status bar.

    When you want to change the status message, just emit that signal:
    Qt Code:
    1. emit updateStatus(strError,15000);
    To copy to clipboard, switch view to plain text mode 

    The slot in the GUI:
    Qt Code:
    1. void GUIClass::updateSlot(QString msg, int code )
    2. {
    3. statusWidget->message(msg,code);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Something like this.

    When emitting signals across threads, they are actually posted as events in the destination threads.

    So your signal will become an event that will cause the slot in the GUI thread to execute asynchronously.
    The point is to let the GUI thread handle modifications in the widgets.
    The slot will execute in the GUI thread's context.

    Regards

  3. #3
    Join Date
    May 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Threading Issue

    Yes,from worker thread..

    Thank you marcel

    Regards
    Noufal

  4. #4
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Threading Issue

    Novice question. Should you wrap the slot code that displays the status in a QMutex or is this handled by the "event" model.

    Pete

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threading Issue

    Well, not really.
    Consider this case: two signals are emitted one after another from the thread.
    This means two events being posted in the GUI event queue.

    Now, these two events will be handled successively, no matter what, even on multiprocessor architectures. There is no way the event handler will handle two events in the same time.
    So, the slot is not reentrant in this case.
    First it is executed for one of the events, then for the other one.


    Regards

Similar Threads

  1. QtPlugin compile issue
    By croland in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2007, 16:37
  2. QMainWindow Maximization Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 15th March 2007, 13:30
  3. qt3 to qt4 - uic issue
    By hvengel in forum Qt Programming
    Replies: 10
    Last Post: 4th March 2007, 02:59
  4. Replies: 5
    Last Post: 22nd September 2006, 08:04
  5. Qt 3.3.4 -> Qt 4.1.2, moc file issue
    By philski in forum Qt Tools
    Replies: 1
    Last Post: 11th September 2006, 21:08

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.