Results 1 to 4 of 4

Thread: QWidget and thread safety

  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QWidget and thread safety

    I need to call certain methods on a QWidget from a thread other than the main GUI thread. I understand this isn't safe, but it is safe to post events to a QWidget that can be processed asynchronously.

    At the moment the only methods I need to call in this fashion are width() height() update() and resize(...), but this may grow.

    Is there a page of documentation somewhere which maps method calls to equivalent QEvents which could be posted to the same effect?

    I expect width() and height() are safe, if by accident rather than design, and there's a promising QResizeEvent, but what about update()?

    Is this a sensible solution? Or should I perhaps subclass QEvent to store the desired calls? Or something else?

    Thanks for your advice

  2. #2
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget and thread safety

    Perhaps I should add, my experiments with Events has been a bit iffy so far,
    this:

    Qt Code:
    1. QObject * target = parent() ? parent() : this;
    2. QEvent * event = new QResizeEvent(QSize(w,h),size());
    3. QApplication::postEvent (target, event);
    To copy to clipboard, switch view to plain text mode 

    has no effect (parent is a QMainWindow), and this:

    Qt Code:
    1. QApplication::postEvent (this, new QPaintEvent (QRect(0,0,width(),height())));
    To copy to clipboard, switch view to plain text mode 

    Causes a "QPainter::begin: Widget painting can only begin as a result of a paintEvent" message to be printed.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget and thread safety

    docs:
    Resize events are sent to widgets that have been resized.
    So I would not assume that sending a QResizeEvent will resize a widget.

    Moreover, being "lucky" on height() etc is not something I would want to depend upon.
    I suggest you just (if nec. create and) call the appropriate slots on the widget.
    If it is just one widget (maybe your own class): easy. just add the slots.

    If it is a widget with "closed" source (for you), you can create a helper class that has those slots and then forwards the calls to the widget you want to modify.

    HTH

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

    Default Re: QWidget and thread safety

    You can create your own artificial events that you will send back and forth between threads to ask and respond to queries but this might be a significant amount of work if the number of methods you want to call grows. Honestly I don't see a reason why would anyone want to know the geometry of some widget from within another thread...

Similar Threads

  1. Non-GUI thread
    By foxyproxy in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 13:18
  2. QWidget with "encapsulated thread"
    By phixx in forum Qt Programming
    Replies: 5
    Last Post: 3rd March 2008, 19:42
  3. Replies: 3
    Last Post: 27th February 2008, 08:01
  4. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 12:32

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.