简体   繁体   English

Q_OBJECT 在我的 QQuickApp 中导致解析错误

[英]Q_OBJECT causes a parse error in my QQuickApp

I'm working on a class derived from QObject, I'm compiling for android and using android qt6.2.2 Clang arm64_v8a kit. I'm working on a class derived from QObject, I'm compiling for android and using android qt6.2.2 Clang arm64_v8a kit. To the default QQuickApp in qt creator I just added the following header file对于 qt 创建者中的默认 QQuickApp,我刚刚添加了以下 header 文件

MyObject.h我的对象.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject : public QObject {
    Q_OBJECT
public:

signals:

public slots:

private:

};

#endif // MYOBJECT_H

main.cpp主文件

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <MyObject.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(u"qrc:/ParseErrorWorkOn/main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml main.qml

import QtQuick

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
}

this is the *.pro file:这是 *.pro 文件:

QT += quick
QT += core

SOURCES += \
        main.cpp

resources.files = main.qml 
resources.prefix = /$${TARGET}
RESOURCES += resources

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

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

HEADERS += \
    MyObject.h

MyObject has signals and slots then I added Q_OBJECT , but compiling I got the following error MyObject 有信号和插槽,然后我添加了Q_OBJECT ,但编译时出现以下错误

error: Parse error at "__attribute__"
make: *** [Makefile:700: moc_MyObject.cpp] Error 1
make: *** Waiting for unfinished jobs.... 

I also compiled with Mingw64 for windows and it works fine.我还用 Mingw64 为 windows 编译,它工作正常。 I cleaned up and reran qmake but nothing changed.我清理并重新运行 qmake 但没有任何改变。 Commenting Q_OBJECT it compiles fine, but I need signals and slots.评论Q_OBJECT它编译得很好,但我需要信号和插槽。 How can I fix this?我怎样才能解决这个问题? thank you in advance.先感谢您。

I figured this out, I made a mistake setting qmake system behaviour() when parsing on use global setting instead to set it on run in the project configuration window.我想通了,我在解析use global setting qmake system behaviour() when parsing而不是在项目配置 window 中将其设置为run 在此处输入图像描述

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

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