Gerry Woodberg
Gerry Woodberg

Reputation: 336

QML communicating with C++

I am having problems with QML communicating with C++. I had followed all the steps expected to make the sample code to run properly. After some hours working on this small example, it comes down to one error message, that I simply can't get rid off:

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

I read some previous posts on related problem, double checking everything, and apparently everything looks correct. Here are the details:

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

The connect, I am using is from the qtproject example:

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

Maybe someone could have some clue what is missing here? Thanks!

Upvotes: 5

Views: 1388

Answers (1)

timday
timday

Reputation: 24892

Will the real class Input please stand up?

You seem to have one defined in Input.h, and another one in Input.cpp. Only one of them is a Q_OBJECT and QObject subclass. main.cpp is seeing the other one from Input.h, so it's entirely unsurprising it can't connect it.

See e.g this tutorial for how to correctly split c++ class declarations and definitions between header and source files, if you're unfamiliar with this area of C++.

Upvotes: 7

Related Questions