Results 1 to 7 of 7

Thread: How to know the signal which caused a qstate transition

  1. #1

    Default How to know the signal which caused a qstate transition

    I have defined a qstatemachine which contains a qstate q1 that can be accessed from several different qstates, however, once reached the qstate q1 the program behaviour must be different depending on the signal which invoke the transition, nevertheless I do not know how to find out which signal caused the transition, since the entered signal of the qstate does not report this.
    Moreover, sometimes I can access the qstate q1 from the same state q2 by different signals (so it's not enough for me to know what the latest state was, I need to know which signal caused the transition)

    Does anyone know how to do that?

  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: How to know the signal which caused a qstate transition

    You can define separate transitions for different origin states leading to the same state.
    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

    Default Re: How to know the signal which caused a qstate transition

    That could be a solution but it would highly increase the number of states of my system, and, unfortunately, my state machine already has around 40 states. If there is no other choice I will do it but I prefer to ask a bit more to find a better solution.

    My system starts from a initial state that only waits for different commands from the user (for example, start process1, start process2, start process3...). Every time this state receives a command, the workflow is more or less the same, the statemachine jumps to a new state called "waiting Ack", and there, a ping is sent (via socket) to the system in charge of executing the selected process. Once an ack answer is received from the system pinged, the statemachine jumps to the state associated to execute the process selected and once finished it returns to the initial state. According to your solution I should have as many "waiting ack" states as types of process are, but if I could know what transition lead to the single "waiting Ack" state, I could unify all the "waiting Ack" small states in only one by inserting a switch in the entered state slot.

    Any idea or suggestion to design my system?

  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: How to know the signal which caused a qstate transition

    Quote Originally Posted by ClintEastwood View Post
    That could be a solution but it would highly increase the number of states of my system
    No, not really.

    Qt Code:
    1. QState *s1 = ...;
    2. QState *s2 = ...;
    3. QSignalTransition *t1 = s1->addTransition(obj1, SIGNAL(signal1()), s2);
    4. QSignalTransition *t2 = s1->addTransition(obj2, SIGNAL(signal2()), s2);
    5.  
    6. connect(t1, &QAbstractTransition::triggered, []() { qDebug() << "t1 fired"; }); // C++11
    7. connect(t2, &QAbstractTransition::triggered, []() { qDebug() << "t1 fired"; }); // C++11
    To copy to clipboard, switch view to plain text mode 
    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
    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 know the signal which caused a qstate transition

    Alternatively you could have separete "start process" states, each using assignProperty to set the correct recipient for the ACK step, e.g. network address and port number.
    Then you transition into a single waiting ACK state which uses the previously set value.

    Cheers,
    _

  6. #6

    Default Re: How to know the signal which caused a qstate transition

    That looks promissing! I'm going to try it. Thank you!

  7. #7

    Default Re: How to know the signal which caused a qstate transition

    I'm trying to compile the following code according to wysota suggestion and I'm getting an error:

    Qt Code:
    1. QState *s1 = new QState();
    2. QState *s2 = new QState();
    3. QSignalTransition *t1 = s1->addTransition(this, SIGNAL(t1_sg()), s2);
    4. QObject::connect(t1, SIGNAL(triggered()), this, SLOT(t1_slot()));
    To copy to clipboard, switch view to plain text mode 

    This is the error:

    'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : can't convert from 'QSignalTransition *' to 'const QObject *'

    As far as I know QSignalTransition inherits from QObject, why can't it convert it?

    I'm trying to compile the following code according to wysota suggestion and I'm getting an error:

    Qt Code:
    1. QState *s1 = new QState();
    2. QState *s2 = new QState();
    3. QSignalTransition *t1 = s1->addTransition(this, SIGNAL(t1_sg()), s2);
    4. QObject::connect(t1, SIGNAL(triggered()), this, SLOT(t1_slot()));
    To copy to clipboard, switch view to plain text mode 

    This is the error:

    'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : can't convert from 'QSignalTransition *' to 'const QObject *'

    As far as I know QSignalTransition inherits from QObject, why can't it convert it?


    Added after 11 minutes:


    I have found the problem, I had included this:

    Qt Code:
    1. #include <QAbstractTransition>
    To copy to clipboard, switch view to plain text mode 

    instead of:

    Qt Code:
    1. #include <QSignalTransition>
    To copy to clipboard, switch view to plain text mode 
    Last edited by ClintEastwood; 24th April 2013 at 15:27.

Similar Threads

  1. Replies: 1
    Last Post: 7th August 2010, 22:38
  2. Replies: 4
    Last Post: 25th March 2010, 10:32
  3. Crash caused by QVariant (mis)use
    By mclark in forum Newbie
    Replies: 2
    Last Post: 31st October 2006, 15:05
  4. Weird crash caused by a QTreeView
    By vfernandez in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2006, 18:31

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.