0

I want to pass variables from my C++ class to a function in my QML class so that the function can use them.

I am trying to use a signal and connection but it's not working.

Here's my relevant code:

main

...
qmlRegisterType<Client>("Client", 1, 0, "Client");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/map.qml")));
...

cpp header

class Client : public QDialog
{
    Q_OBJECT

public:
    explicit Client(QWidget *parent = nullptr);
...

public slots:
    void callDoAddMarker(float latitude, float longitude);

signals:
    void doAddMarker(float latitude, float longitude);

cpp class

void Client::someFunction(UINT16 array[]) {

    ...

    callDoAddMarker(latit, longit);
}

void Client::callDoAddMarker(float latitude, float longitude) {
    emit doAddMarker(latitude, longitude);
}

and in QML

import Client 1.0

...

function addMarker(latitude, longitude) {
    ...
}

Client {
    id: client
}

Connections {
    target: client
    onDoAddMarker: {addMarker(latitude, longitude)}
}

Can someone tell me what I'm doing wrong? Any help is greatly appreciated!

EDIT: Adding in my main to show how I register the cpp class with QML, not sure if this is correct.

jb_1997
  • 1
  • 1
  • Please show how you have created `client` available in QML – Amfasis Jun 19 '20 at 12:12
  • you need to register your type to be used in QML. There are [several ways](https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#choosing-the-correct-integration-method-between-c-and-qml) to do that. Read more [here](https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type). Anyway I wouldn't be sure about QWidget class ... – folibis Jun 19 '20 at 12:13
  • Are you getting any error messages? Have you added any logging to the signal handler indicating if it is getting called correctly? – David K. Hess Jun 19 '20 at 15:52
  • No error messages, I will try to add some logging. – jb_1997 Jun 19 '20 at 16:44
  • maybe signal-slot? – eri Jun 20 '20 at 10:25

0 Answers0