1

在这里,我使用的是 Qt Creator,今天当我尝试编译这段代码时,它碰巧导致了一些错误。代码:

#include "mainwindow.h"

#include <QApplication>
#include <QPushButton>
#include <QGraphicsItem>

#include <QtStateMachine/QState>
#include <QtStateMachine/QStateMachine>

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

    QPushButton button("State Machine");
    QStateMachine machine;
    QState* s1 = new QState(&machine);
    QState* s2 = new QState(&machine);
    QState* s3 = new QState(&machine);
    s1->assignProperty(&button, "geometry", QRect(100, 100, 100, 50));
    s2->assignProperty(&button, "geometry", QRect(300, 100, 100, 50));
    s3->assignProperty(&button, "geometry", QRect(200, 200, 100, 50));

    s1->addTransition(&button, SIGNAL(clicked()), s2);
    s2->addTransition(&button, SIGNAL(clicked()), s3);
    s3->addTransition(&button, SIGNAL(clicked()), s1);

    machine.setInitialState(s1);
    machine.start();
    button.show();

    return a.exec();
}

错误如下

D:\Users\STRING10\Documents\QtProjects\build-QStateMachineTest-Desktop_Qt_6_1_0_MinGW_64_bit-Debug..\QStateMachineTest\main.cpp:15: error: undefined reference to \`__imp__ZN13QStateMachineC1EP7QObject' debug/main.o: In function \`qMain(int, char**)': D:\Users\STRING10\Documents\QtProjects\build-QStateMachineTest-Desktop_Qt_6_1_0_MinGW_64_bit-Debug/../QStateMachineTest/main.cpp:15: undefined reference to \`__imp__ZN13QStateMachineC1EP7QObject'

我试图找出我是否错过了存储库,但它确实存在于我的计算机中,并且我已将编译器设为 MinGW,它确实有一个名为 QState 的存储库。

这是我的.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
4

1 回答 1

2

在 Qt6 QStateMachine(和其他类似的类)属于 QtStateMachine 子模块,不再属于 QtCore 子模块,所以添加QT += statemachine到 .pro 中。

于 2021-05-16T23:25:39.013 回答