0

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));
    ??????????????????????????????
}
Oliver
  • 59
  • 1
  • 8

1 Answers1

3

Did you try initializing MyBacklogg by passing the object 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.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • No I haven't, but if what you're saying its true, it might not even be worth a chance. If going with a signal and a handler is better? @dtech – Oliver Nov 30 '17 at 12:40
  • Think of C++ as the self contained inner core layer of your application. Something QML only interfaces with. QML should access and drive C++ stuff, not the other way around. There are very few and far in between cases where the opposite makes sense and is unavoidable. And it is best to avoid it as much as possible in order to prevent bad design growing and eventually ruining your application, forcing you to go back and redesign stuff. – dtech Nov 30 '17 at 12:48
  • Okey, this is my first time working with qt, qml and c++ so am a bit confused. How can I access a children of my engine.rootObjects? Not sure what to put as type, since these are from QtQuick items. If I want to do a connect to MyMap and its signal instead of main.qml and its signal. – Oliver Nov 30 '17 at 13:35
  • You expose some C++ object to QML and let the connection and binding and all the other stuff to QML. Remember: `enigne.rootObjects` is living in C++ - and that is where you don't want to access the children of it. – derM - not here for BOT dreams Nov 30 '17 at 13:40
  • okey, is it better to have the string or so as a parameter in my C++ class, and do a qmlRegiser in my main.cpp, and then just show the value of that parameter in my GUI? – Oliver Nov 30 '17 at 14:24
  • You can emit the data with the signal. – dtech Nov 30 '17 at 15:00
  • But if you try to do it, as the Qtees do it, yeah, you would just expose it as a `Q_PROPERTY` with a `NOTIFY` signal. This empowers you to use the value in many cases purely declaratively and when necessary you can still access it in a signal handler. – derM - not here for BOT dreams Nov 30 '17 at 15:26