QML与C++通信

Ger*_*erg 5 c++ qt qml

我遇到QML与C++通信的问题.我已经按照预期的所有步骤使示例代码正常运行.在这个小例子工作几个小时后,它归结为一条错误信息,我根本无法摆脱:

./input/main.cpp:18: error: 
        no matching function for call to 
        'QObject::connect(QObject*&, const char*, Input*, const char*)'
                   &input, SLOT(cppSlot(QString)));
                                                 ^
Run Code Online (Sandbox Code Playgroud)

我读了一些以前关于相关问题的帖子,仔细检查了一切,显然一切看起来都正确.以下是详细信息:

./Sources/main.cpp

#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlEngine>
#include <QQuickWindow>
#include <QtDeclarative>
#include <QObject>
#include <QDebug>
#include "Input.h"

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

 QDeclarativeView view(QUrl::fromLocalFile("input.qml"));
 QObject *item = view.rootObject();

 Input input;
 QObject::connect(item, SIGNAL(qmlSignal(QString)),
                  &input, SLOT(cppSlot(QString)));
 view.show();
 return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

./Headers/Input.h

#ifndef INPUT_H
#define INPUT_H
#include <QObject>
#include <QDebug>
class Input : public QObject
{  public:
     Input(){} // default constructor
     Q_OBJECT
   public slots:
     void cppSlot(const QString &msg) {
                  qDebug() << "Called the C++ slot with message:" << msg;
          }

};
#endif // INPUT_H
Run Code Online (Sandbox Code Playgroud)

./Input.pro

QT += qml quick sensors xml
QT += declarative
SOURCES += \
    main.cpp \
    Input.cpp
RESOURCES += \
    Input.qrc
OTHER_FILES += \
    Input.qml
HEADERS += \
    Input.h
Run Code Online (Sandbox Code Playgroud)

./Resources/Input.qrc

/
Input.qml
Run Code Online (Sandbox Code Playgroud)

我使用的连接来自qtproject示例:

QObject::connect(item, SIGNAL(qmlSignal(QString)),&myClass, SLOT(cppSlot(QString))); 
Run Code Online (Sandbox Code Playgroud)

也许有人可以有一些线索在这里缺少什么?谢谢!

tim*_*day 7

真的class Input请站起来吗?

您似乎在Input.h中定义了一个,在Input.cpp中定义了另一个.其中只有一个是Q_OBJECT和QObject子类.main.cpp正在从Input.h中看到另一个,所以它无法连接它完全不足为奇.

如果您不熟悉C++的这个领域,请参阅本教程,了解如何在头文件和源文件之间正确分割c ++类声明和定义.