具有自定义类型的 Qt Signal

ger*_*rum 5 c++ qt qml

我想用 Qt 发送带有参数的信号。该参数应该是一个自定义的结构体。我已经用 Q_DECLARE_METATYPE 注册了它,但它不起作用,我收到一些奇怪的编译错误。

她是我的代码:

助手.h

#ifndef HELPER_H
#define HELPER_H

#include <QString>
#include <QMetaType>
struct result{
    QString info;
bool sugestion;
};
Q_DECLARE_METATYPE(result)

#endif // HELPER_H
Run Code Online (Sandbox Code Playgroud)

分析器.h

#ifndef ANALYSER_H
#define ANALYSER_H
#include "helper.h"
#include <QObject>

class analyser: public QObject
{
    Q_OBJECT
public:
    void test()
    {
        result ret;
        ret.info="Hallo";
        emit show(ret);
    }
signals:
    void show(result r);
};

#endif // ANALYSER_H
Run Code Online (Sandbox Code Playgroud)

qmlbackend.h

#ifndef QMLBACKEND_H
#define QMLBACKEND_H

#include <QObject>
#include "helper.h"
#include <QDebug>

class QmlBackend : public QObject
{
public slots:
    void hit(result res)
    {
        qDebug()<<"Working"<<res.info;
    }
 };

 #endif // QMLBACKEND_H
Run Code Online (Sandbox Code Playgroud)

主程序

#include <QDebug>
#include "qmlbackend.h"
#include "analyser.h"
#include <QGuiApplication>


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

    QmlBackend b;
    analyser t;
    QObject::connect(&t,&analyser::show,&b,&QmlBackend::hit);
    t.test();

    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

moc_analyser.cpp: In static member function 'static void analyser::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
moc_analyser.cpp:91:49: error: cannot declare pointer to 'void' member
         typedef void (analyser::*_t)(result );
                                             ^
moc_analyser.cpp:91:49: error: typedef '_t' is initialized (use decltype instead)
moc_analyser.cpp:92:35: error: '_t' does not name a type; did you mean '_o'?
         if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&analyser::show)) {
                               ^~
moc_analyser.cpp:92:38: error: expected '>' before '*' token
         if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&analyser::show)) {
                                  ^
moc_analyser.cpp:92:38: error: expected '(' before '*' token
moc_analyser.cpp:92:39: error: expected primary-expression before '>' token
         if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&analyser::show)) {
                                   ^
moc_analyser.cpp:92:62: error: '_t' does not name a type; did you mean '_o'?
         if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&analyser::show)) {
                                                          ^~
                                                          _o
moc_analyser.cpp:92:84: error: expected ')' before '{' token
         if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&analyser::show)) {
                                                                                ^
moc_analyser.cpp:96:9: error: expected primary-expression before '}' token
     }
     ^
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么它不起作用或者我必须改变什么?

eyl*_*esc 5

这有点奇怪,证明您的代码会生成相同的错误,但如果我将结构名称更改resultResult编译并正确执行(我建议您删除构建文件夹,然后编译)。

我正在寻找 Qt 文档,其中指出使用时数据类型应大写Q_DECLARE_METATYPE