Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to return to a previous state using QStateHistory with nested states?
Forum Updated to NodeBB v4.3 + New Features

How to return to a previous state using QStateHistory with nested states?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 745 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matio
    wrote on last edited by SGaist
    #1

    Hello,

    I work on a state machine with QState.

    I have a problem about returning to a previous state with the class QHistoryState.

    Here is a diagram of the part that poses me problem:

    Diagram here

    For example I would like that when I use the interruption that allows to go from S13 to S3, when returning to the previous state, I return to S13 (the previous state).

    But because I have defined S11 as the initial state of S1, the state machine returns to S11.

    What is the best way to do that, knowing that I want to be able to do the same thing from S22 for example.

    [edit: Fixed image link SGaist]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show the code you use to create the state machine and manage the history state ? From my understanding of the class, it should return to the last child state it was in unless it never entered the parent state, in which case it will use the default that you set on it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        matio
        wrote on last edited by
        #3

        Hi,

        Here is the main part of the cpp file:

        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            machine=new QStateMachine();
        
            s3 = new QState();
        
            s0 = new QState();
        
            s1 = new QState(s0);
        
            s2 = new QState(s0);
        
            s11 = new QState(s1);
            s12 = new QState(s1);
            s13 = new QState(s1);
        
            s21 = new QState(s2);
            s22 = new QState(s2);
            s23 = new QState(s2);
        
            s1h=new QHistoryState(s1);
            s2h=new QHistoryState(s2);
            s0h=new QHistoryState(s0);
        
        
            s11->addTransition(ui->pb_s12, SIGNAL(clicked()), s12);
            s12->addTransition(ui->pb_s13, SIGNAL(clicked()), s13);
            s13->addTransition(ui->pb_s11, SIGNAL(clicked()), s11);
        
            s21->addTransition(ui->pb_s22, SIGNAL(clicked()), s22);
            s22->addTransition(ui->pb_s23, SIGNAL(clicked()), s23);
            s23->addTransition(ui->pb_s21, SIGNAL(clicked()), s21);
        
            s0->addTransition(ui->pb_s3, SIGNAL(clicked()), s3);
            s3->addTransition(ui->pb_s3, SIGNAL(clicked()), s0h);
        
            s1->addTransition(ui->pb_s2, SIGNAL(clicked()), s2);
            s2->addTransition(ui->pb_s2, SIGNAL(clicked()), s1h);
        
            s1->setInitialState(s11);
            s2->setInitialState(s21);
            s0->setInitialState(s1);
        
            machine->addState(s0);
            machine->addState(s3);
        
            machine->setInitialState(s0);
        
            connect(s0, SIGNAL(entered()), this, SLOT(enteredS0()));
            connect(s0, SIGNAL(exited()), this, SLOT(exitedS0()));
        
            connect(s1, SIGNAL(entered()), this, SLOT(enteredS1()));
            connect(s1, SIGNAL(exited()), this, SLOT(exitedS1()));
        
            connect(s11, SIGNAL(entered()), this, SLOT(enteredS11()));
            connect(s11, SIGNAL(exited()), this, SLOT(exitedS11()));
        
            connect(s12, SIGNAL(entered()), this, SLOT(enteredS12()));
            connect(s12, SIGNAL(exited()), this, SLOT(exitedS12()));
        
            connect(s13, SIGNAL(entered()), this, SLOT(enteredS13()));
            connect(s13, SIGNAL(exited()), this, SLOT(exitedS13()));
        
            connect(s2, SIGNAL(entered()), this, SLOT(enteredS2()));
            connect(s2, SIGNAL(exited()), this, SLOT(exitedS2()));
        
            connect(s21, SIGNAL(entered()), this, SLOT(enteredS21()));
            connect(s21, SIGNAL(exited()), this, SLOT(exitedS21()));
        
            connect(s22, SIGNAL(entered()), this, SLOT(enteredS22()));
            connect(s22, SIGNAL(exited()), this, SLOT(exitedS22()));
        
            connect(s23, SIGNAL(entered()), this, SLOT(enteredS23()));
            connect(s23, SIGNAL(exited()), this, SLOT(exitedS23()));
        
            connect(s3, SIGNAL(entered()), this, SLOT(enteredS3()));
            connect(s3, SIGNAL(exited()), this, SLOT(exitedS3()));
        
            machine->start();
        }
        

        When the machine leave s3, it always return to s11 or s21.

        I've uploaded the complete project here:
        https://1fichier.com/?rh22hz2sx8o0z98y840m

        Thanks for your help!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          matio
          wrote on last edited by
          #4

          OK, I found what was wrong.

          I needed to set the history type with this function:

          s0h->setHistoryType(QHistoryState::DeepHistory);
          

          from the documentation page of QHistoryState:

          https://doc.qt.io/qt-5/qhistorystate.html

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved