I want to invoke a method in MyMap
from my class MyBacklogg
, how do I do this when I don't have the object parameters in that class?
I'm going to receive a string, a QByteArray or a QDataStream in MyBacklogg
, depending on which one that works the best, and I want to pass these along to my GUI.
main.cpp:
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/qml/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QObject *item = engine.rootObjects().first();
MyTcpSocket s;
s.init();
QObject::connect(item, SIGNAL(sendSignal()), &s, SLOT(doSlot()));
return app.exec();
}
main.qml:
ApplicationWindow {
id: appWindow
visible: true
height: 600
width: 800
title: qsTr("MyApp")
signal sendSignal()
RowLayout{
visible: true
anchors.fill: parent
MyMap {
id: mapview
function myFunc() {
//function called from MyBacklogg
}
function myOtherFunc() {
sendSignal()
}
}
}
}
MyBacklogg.cpp:
#include "mybacklogg.h"
MyBacklogg::MyBacklogg(QObject *parent) :
QObject(parent)
{
}
void MyBacklogg::init()
{
//initialize
}
void MyBacklogg::doSlot()
{
//function call from MyMap
}
void MyBacklogg::callMethod()
{
??????????????????????????????
QMetaObject::invokeMethod(object, "myFunc",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, msg));
??????????????????????????????
}
Did you try initializing
MyBacklogg
by passing theobject
pointer to it and preserving it as a class member?Although if you are calling QML functions from C++, in 99.9% of the cases you are doing it wrong.
The proper solution would be to emit a signal from C++ and install a handler for it on the QML side.