【问题标题】:Q_OBJECT causes a parse error in my QQuickAppQ_OBJECT 在我的 QQuickApp 中导致解析错误
【发布时间】:2021-12-31 11:15:38
【问题描述】:

我正在研究从 QObject 派生的类,我正在为 android 编译并使用 android qt6.2.2 Clang arm64_v8a 套件。在 qt creator 中的默认 QQuickApp 中,我刚刚添加了以下头文件

MyObject.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

import QtQuick

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

这是 *.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 有信号和槽然后我添加了Q_OBJECT,但编译时出现以下错误

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

我也用 Mingw64 为 windows 编译,它工作正常。 我清理并重新运行 qmake 但没有任何改变。 评论 Q_OBJECT 它编译得很好,但我需要信号和插槽。我怎样才能解决这个问题?提前谢谢你。

【问题讨论】:

    标签: c++ makefile qt-quick qt-signals qt6


    【解决方案1】:

    我想通了,我在项目配置窗口中将qmake system behaviour() when parsing 设置为use global setting 而不是设置为run 时出错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多