【问题标题】:QML communicating with C++QML 与 C++ 通信
【发布时间】:2013-12-30 10:35:14
【问题描述】:

我在使用 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)));
                                                 ^

我在related problem 上阅读了一些以前的帖子,仔细检查了所有内容,显然一切看起来都是正确的。以下是详细信息:

./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();
}

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

./Input.pro

QT += qml quick sensors xml
QT += declarative
SOURCES += \
    main.cpp \
    Input.cpp
RESOURCES += \
    Input.qrc
OTHER_FILES += \
    Input.qml
HEADERS += \
    Input.h

./Resources/Input.qrc

/
Input.qml

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

QObject::connect(item, SIGNAL(qmlSignal(QString)),&myClass, SLOT(cppSlot(QString))); 

也许有人可以知道这里缺少什么?谢谢!

【问题讨论】:

    标签: c++ qt qml


    【解决方案1】:

    请真正的class Input站起来好吗?

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

    如果您不熟悉 C++ 的这一领域,请参阅 tutorial,了解如何在头文件和源文件之间正确拆分 C++ 类声明和定义。

    【讨论】:

    • 谢谢。我注释掉了整个 Imput.h,并将 evertyhing 移到 Input.cpp 中。然后编译器给出: ./Input.cpp:8: error: invalid use of incomplete type 'class QDebug' qDebug()
    • @timday 表示#include &lt;QDebug&gt; 而不是#include &lt;QtDebug&gt;。 ;-)
    • 您要么缺少 Q_OBJECT 和/或缺少源文件末尾的 #include "Input.moc" 包含...或者,您缺少添加到 HEADERS 变量的 Input.h。此外,您不需要在标头中包含 qdebug,因为它没有在其中使用。
    • @timday。注意到了。谢谢指点那个人!现在我解决了在我的帖子中更新的类双重声明。只有错误仍然存​​在。
    • @GerryWoodberg:您的代码仍然是错误的。另外,如果你使用 QtCreator,make qmake 会显式地重新运行。
    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多