Results 1 to 6 of 6

Thread: multithreaded gui updates

  1. #1
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default multithreaded gui updates

    I need to update the GUI from multiple threads. So far I've taken the following approach:
    Qt Code:
    1. connect(this, SIGNAL(setGUILabel(QString)), label, SLOT(setText(QString const&)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    and it works... What bothers me is that the argument of the signal is of QString type (so that I make a copy of the string in the signal issuing thread), but the argument of the slot is QString const&. Is this mixing of references and object types allowed by Qt?

  2. #2
    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: multithreaded gui updates

    Two things. First is that you are not updating the gui from multiple threads, the gui is updated from one thread only. Second, signals undergo normalization procedure, passing const reference and copy results in the same normalized signature, so you don't have to worry. You can switch your two signatures (use const reference in the signal and copy in the slot) in your connect statement and it will still work.
    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.


  3. #3
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default Re: multithreaded gui updates

    Well, I can't switch the signatures, as the slot signature inside QLabel is fixed. As for updating the GUI from multiple threads: I'm emitting the signal for that reason.

  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: multithreaded gui updates

    Quote Originally Posted by ugluk View Post
    Well, I can't switch the signatures, as the slot signature inside QLabel is fixed.
    No, you don't understand. I mean that instead of:
    Qt Code:
    1. connect(this, SIGNAL(setGUILabel(QString)), label, SLOT(setText(QString const&)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    you can write:
    Qt Code:
    1. connect(this, SIGNAL(setGUILabel(const QString &)), label, SLOT(setText(QString)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    or even:
    Qt Code:
    1. connect(this, SIGNAL(setGUILabel(QString)), label, SLOT(setText(QString)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    You don't need to modify the methods themselves.
    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.


  5. #5
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default Re: multithreaded gui updates

    I know that QString copies are shallow, when possible, but I think references are still better than copying. I think what you describe is counterintuitive - if a slot accepts a reference, then Qt should actually deliver one, in my opinion. Where can I read more on signal normalization?

  6. #6
    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: multithreaded gui updates

    You want a delayed invokation, you can't deliver a reference because it might not exist at the time when the slot is executed. All delayed calls cause data serialization and deserialization. Besides, I'm not talking about anything related to how a method is actually called. I'm only saying "const QString &" and "QString" will be both normalized to "QString" when signal and slot signatures are being compared.
    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.


Similar Threads

  1. QGLWidget multithreaded example?
    By danielperaza in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2011, 08:15
  2. Multithreaded server
    By virus in forum Newbie
    Replies: 1
    Last Post: 23rd February 2011, 08:28
  3. Multithreaded OpenGL
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 17:55
  4. Qt + gprof in a multithreaded app?
    By papercut in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 18:12
  5. Designing Multithreaded app in Qt
    By summer_of_69 in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2009, 16:46

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.