I am using QStateMachine framework for a device controller class. It works fine in debug mode. But, in the release mode QStateMachine::started() signal is not being emitted. A simple widget project for the problem (form is empty) is below.
Qt Version 5.14.1
Compiler : MSVC 2017, MinGW (both are 64-bit and results are same)
Test.pro
QT = core gui widgets
CONFIG = c 11
DEFINES = QT_DEPRECATED_WARNINGS
SOURCES = main.cpp mainwindow.cpp
HEADERS = mainwindow.h
FORMS = mainwindow.ui
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStateMachine>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void stateMachineStarted();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStateMachine *stateMachine;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
void MainWindow::stateMachineStarted()
{
qDebug() << "MainWindow::stateMachineStarted()";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
qDebug() << "MainWindow::MainWindow()";
ui->setupUi(this);
stateMachine = new QStateMachine;
QState *closed = new QState;
QState *setup = new QState;
QState *opened = new QState;
QState *closing = new QState;
stateMachine->addState(closed);
stateMachine->addState(setup);
stateMachine->addState(opened);
stateMachine->addState(closing);
Q_ASSERT(connect(stateMachine, &QStateMachine::started, this, &MainWindow::stateMachineStarted));
stateMachine->setInitialState(closed);
stateMachine->start();
}
MainWindow::~MainWindow()
{
qDebug() << "MainWindow::~MainWindow()";
delete ui;
}
Application output in debug mode (I closed the form after few seconds.)
12:39:09: Starting C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Test.exe ...
MainWindow::MainWindow()
MainWindow::stateMachineStarted()
MainWindow::~MainWindow()
12:39:11: C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Test.exe exited with code 0
Application output in release mode (I closed the form after few seconds.)
12:27:51: Starting C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Release\release\Test.exe ...
MainWindow::MainWindow()
MainWindow::~MainWindow()
12:27:53: C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Release\release\Test.exe exited with code 0
CodePudding user response:
In release mode, it is likely that the connection is not made because you wrapped it inside a Q_ASSERT
macro.
See Q_ASSERT release build semantics for more informations.