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. Cannot create Qstatemachine inside constructor
Qt 6.11 is out! See what's new in the release blog

Cannot create Qstatemachine inside constructor

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 913 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.
  • I Offline
    I Offline
    ion_knight
    wrote on last edited by
    #1

    Ok getting a problem where the state will not change when inside a constructor. When it's in main it works fine, so at a bit of a loss really now. If anyone has an idea what I am doing wrong please let me know.

    main.cpp
    @#include "widget.h"
    #include <QApplication>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QStateMachine>
    #include <QState>
    #include <QRectF>
    #include <QLabel>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    Widget w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    widget.cpp
    @#include "widget.h"
    #include "ui_widget.h"

    #include <QApplication>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QStateMachine>
    #include <QState>
    #include <QRectF>
    #include <QLabel>

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);

    QWidget *window = new QWidget(this);
    
    /* Here created the buttons */
    
    QPushButton *button1 = new QPushButton ("1");
    //QLabel *Current_State(&window);
    QLabel *Current_State= new QLabel;
    Current_State->setText("NO STATE");
    // QPushButton *button2 = new QPushButton ("2");
    //QPushButton *button3 = new QPushButton ("3");
    
    /* Here created the Layout */
    
    QVBoxLayout  *vbox = new QVBoxLayout;
    
    /* Here the buttons are added  in the Layout */
    
    vbox->addWidget(button1);
    vbox->addWidget(Current_State);
    
    window->setLayout(vbox);
    window->resize(200,100);
    
    /* The state machine is created */
    
    QStateMachine machine;
    
    /* State are created  */
    
    QState *state1 = new QState();
    QState *state2 = new QState();
    QState *state3 = new QState();
    
    state1->assignProperty(Current_State, "text", "In State 1");
    state2->assignProperty(Current_State, "text", "In State 2");
    state3->assignProperty(Current_State, "text", "In State 3");
    
    
    state1->addTransition(button1, SIGNAL(clicked()), state2);
    state2->addTransition(button1, SIGNAL(clicked()),state3);
    state3->addTransition(button1, SIGNAL(clicked()),state1);
    
    /* States are added in state machine */
    
    machine.addState(state1);
    machine.addState(state2);
    machine.addState(state3);
    
    /*  Set the state initial for the state machine */
    
    machine.setInitialState(state1);
    
    /* Start the state machine */
    machine.start();
    

    }

    Widget::~Widget()
    {
    delete ui;
    }@

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      QStateMachine machine is stack variable.

      Try the following

      QStateMachine *machine = new QStateMachine;

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0

      • Login

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