Results 1 to 2 of 2

Thread: How to recover the argument of a signal that triggered a signaltransition...

  1. #1

    Default How to recover the argument of a signal that triggered a signaltransition...

    Hello all, I'm using a QStateMachine to manage my application, and sometimes a signal with an argument triggers a transition and I want to get its argument inside of the transition slot, but I do not know how to. Below you can find an example of my problem:

    Let's assume we have some kind of socket class that emits a signal every time it receives a new request:

    Qt Code:
    1. class MySocket:public QTcpSocket
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ...
    7.  
    8. signals:
    9.  
    10. void newRequest(int request);
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Then inside of my application I have a QStateMachine with this transition:

    Qt Code:
    1. MySocket* mySocket = new MySocket();
    2.  
    3. QStateMachine machine;
    4.  
    5. QState *s1 = new QState();
    6. QState *s2 = new QState();
    7.  
    8. QSignalTransition* s1_s2_transition = s1->addTransition(mySocket, SIGNAL(newRequest(int)), s2);
    9. QObject::connect(s1, SIGNAL(triggered()), this, SLOT(s1_s2_transition_process());
    10.  
    11. machine.addState(s1);
    12. machine.addState(s2);
    To copy to clipboard, switch view to plain text mode 

    What I want to do is to get the int argument emitted with the newRequest signal inside the s1_s2_transition_process() slot:

    Qt Code:
    1. void MyApplication::s1_s2_transition_process()
    2. {
    3. //here I want to get the int value emitted with the newRequest(int) signal
    4. }
    To copy to clipboard, switch view to plain text mode 

    What I did to solve this problem is to store the value in a property in MySocket class before emitting the signal and access it by using a public accesor. This would be MySocket class:

    Qt Code:
    1. class MySocket:public QTcpSocket
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. int lastRequest;
    7.  
    8. int getLastRequest() const {return lastRequest;};
    9.  
    10. signals:
    11.  
    12. void newRequest(int request);
    13. }
    14.  
    15. MySocket::extractRequestFromReceivedBytes()
    16. {
    17. ...
    18. // the request is extracted and stored in data
    19. ...
    20. lastRequest = data;
    21. emit newRequest(lastRequest);
    22. }
    To copy to clipboard, switch view to plain text mode 

    And the slot:

    Qt Code:
    1. void MyApplication::s1_s2_transition_process()
    2. {
    3. int request = mySocket->getLastRequest();
    4. }
    To copy to clipboard, switch view to plain text mode 

    I don't like this solution because I think two signals could be emitted from MySocket class very close in time, the second one could overwrite the lastRequest property before the first slot was invoked, and then it would retrieve the second value twice.

    Any idea of how to solve this issue or how to redesign my system?

    Thanks in advance

  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: How to recover the argument of a signal that triggered a signaltransition...

    Quote Originally Posted by ClintEastwood View Post
    I don't like this solution because I think two signals could be emitted from MySocket class very close in time, the second one could overwrite the lastRequest property before the first slot was invoked, and then it would retrieve the second value twice.
    The second signal can only be processed when you have finished prcessing the first, i.e. when you have given the event loop a chance to process the event that will lead to the next signal's emission.

    Alternatively you can create your own signal transition class that takes the argument from the signal event and emits its own signal.
    See http://qt-project.org/doc/qt-4.8/qsignaltransition.html

    Cheers,
    _

Similar Threads

  1. QUdpSocket readyRead() signal not triggered
    By deionut in forum Newbie
    Replies: 2
    Last Post: 26th October 2010, 18:19
  2. Replies: 2
    Last Post: 20th October 2010, 16:16
  3. Replies: 0
    Last Post: 24th May 2010, 08:11
  4. Replies: 1
    Last Post: 11th April 2010, 14:21
  5. Replies: 1
    Last Post: 29th February 2008, 23:04

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
  •  
Qt is a trademark of The Qt Company.