Results 1 to 4 of 4

Thread: QSignalMapper with one object being a thread (no visual object)

  1. #1
    Join Date
    Jul 2016
    Posts
    18
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default QSignalMapper with one object being a thread (no visual object)

    Hello,

    I have a Dialog Box with a background thread doing work for it. The provided example from Qt used Signals and Slots and did not use QSignalMapper at all which is surprising. Well, upgrading to 5.7, this no longer works. They must have closed a bug or two which is indirectly a good thing for Qt. I now need to make this work the right way. Here's parts of it...

    Qt Code:
    1. class MyDialog : public QDialog
    2. {
    3. Q_OBJECT
    4. ...
    5. public slots:
    6. void showResponse ( const QString &stringResponse );
    7. void processError ( const QString &stringError );
    8. void processTimeout ( const QString &stringTimeout );
    9. ...
    10. }
    11.  
    12. class MyThread : public QThread
    13. {
    14. Q_OBJECT
    15. ...
    16. signals:
    17. void response ( const QString & );
    18. void error ( const QString & );
    19. void timeout ( const QString & );
    20. ...
    21. }
    22.  
    23. The direction of communication is child to parent and is thus a great reason to use signals and slots. But how do I set this up?
    24.  
    25. MyThread::MyDialog ( QQDialog* parent ) <--should i do this in MyThread or MyDialog?
    26. : ...
    27. {
    28. ...
    29. m_signalMapper = new QSignalMapper (); // parent or this?
    30. connect ( &m_threadATMega328p_RS232, SIGNAL ( response ( QString ) ), m_signalMapper, SLOT ( map () ) );
    31. m_signalMapper->setMapping ( this, 0 ); // 0 is an ID but how do you reference 0 or 1 or 2? How do you map this to that?
    32. //connect ( m_signalMapper, SIGNAL ( mapped ( QString ) ), this, SIGNAL ( response ( const QString & ) ) );
    33. connect ( m_signalMapper, SIGNAL ( mapped ( QString ) ), this, SLOT ( showResponse ( QString ) ) );
    34. ...
    35. }
    To copy to clipboard, switch view to plain text mode 

    What I need to do is have three SIGNAL functions in the thread that communicate with three SLOTS in the dialog box. So the setMapping call is important and I am unsure how to make this work with three different and dynamic QStrings. I saw examples but none with threads and surely none with one thread and three signals/slots. I read the help page but it talks about objects that have a function that gives dynamic text. This is not like that as I do have dynamic text as input, not that is callable.

    Thanks for any direction. Have a great day.

    Cheers,
    Pete

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper with one object being a thread (no visual object)

    You just connect the thread's signal to the dialog's slots.

    I guess you have code like this somewhere in your dialog

    Qt Code:
    1. MyThread* thread = new MyThread(this);
    To copy to clipboard, switch view to plain text mode 
    so you just add
    Qt Code:
    1. connect(thread, SIGNAL(response(QString)), this, SLOT(showResponse(QString)));
    To copy to clipboard, switch view to plain text mode 
    and so on.

    Not sure why you would want to use a signal mapper and lose the signal arguments. Discarding the signal arguments can also be done in a normal connection.

    Cheers,
    _

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

    PeterVE (26th July 2016)

  4. #3
    Join Date
    Jul 2016
    Posts
    18
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSignalMapper with one object being a thread (no visual object)

    Thank you. I did exactly that and it worked. What I had used from a Qt example was working until the upgrade. I tried to find examples online using arguments and they stated to use the signal mapper. But your suggestion is what was originally intended and does work. So thank you and have a great day!

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper with one object being a thread (no visual object)

    The signal mapper use case is if you have multiple signal sources without arguments and you want a single slot being called with arguments depending on the signal source.

    E.g. a set of buttons and having the slot called with a numerical value or string depending on which button was clicked.

    Cheers,
    _

Similar Threads

  1. TypeError: Object [object Object] has no method 'sendData'
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 30th November 2013, 05:54
  2. Replies: 6
    Last Post: 25th May 2013, 22:26
  3. Replies: 1
    Last Post: 8th November 2011, 22:27
  4. QSignalMapper One single Object
    By cafu1007 in forum Qt Programming
    Replies: 7
    Last Post: 18th April 2011, 08:46
  5. Accessing ui object from thread
    By khelsys in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2009, 11:05

Tags for this Thread

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.