繁体   English   中英

QStateMachine 在释放模式下没有发出started() 信号

[英]QStateMachine is not emitting started() signal in release mode

我正在为设备 controller class 使用 QStateMachine 框架。 它在调试模式下工作正常。 但是,在发布模式下,不会发出 QStateMachine::started() 信号。 下面是一个针对该问题的简单小部件项目(表单为空)。

Qt 版本 5.14.1
编译器:MSVC 2017,MinGW(均为 64 位,结果相同)

测试.pro

QT += core gui widgets 
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui

主文件

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

主窗口.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

主窗口.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;
}

应用程序 output 处于调试模式(几秒钟后我关闭了表单。)

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

应用程序 output 处于发布模式(几秒钟后我关闭了表单。)

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

在发布模式下,很可能没有建立连接,因为您将它包装在Q_ASSERT宏中。

有关更多信息,请参阅Q_ASSERT 版本构建语义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM