从C 调用QML方法



我想从我的类MyMap中调用CC_1中的方法,当我没有该类中的对象参数时,我该怎么做?

我将在MyBacklogg中收到一个字符串,QByTearray或QDataStream,具体取决于哪个效果最好,我想将其传递给我的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));
    ??????????????????????????????
}

您是否尝试通过传递object指针并将其保存为类成员来初始化MyBacklogg

尽管您正在调用C 的QML函数,但在99.9%的情况下,您做错了。

适当的解决方案是从C 发出信号并在QML侧安装处理程序。

相关内容

  • 没有找到相关文章

最新更新