将QVariant从QML传递到C

Passing QVariant from QML to C++

本文关键字:QML QVariant      更新时间:2023-10-16

我正在尝试将QML列表发送到C 。我已经尝试使用一个字符串和一个成功的整数,但是当我尝试使用QVariant时,我会发现错误:

qobject ::连接:没有这样的插槽myclass :: cppslot(qvariant)在../test_2206/main.cpp:31

我的main.qml

import QtQuick 2.4
import QtQuick.Layouts 1.1
import Material.ListItems 0.1 as ListItem
import Material.Extras 0.1
import QtQuick.Controls 1.3 as QuickControls
import QtQuick.Controls 1.4
import Material 0.2

Window {
    visible: true
    property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
        id: item
        width: 100; height: 100
        signal qmlSignal(variant msg)
       /* MouseArea {
            anchors.fill: parent
            onClicked: item.qmlSignal("Hello from QML")
        }*/
    Rectangle{
        id:sousRect
        color:"red"
        width:100; height:100
            Button{
                id:buttonTest
                onClicked: {
                     item.qmlSignal(listCloud)
                }
            }
    }
}

我的Main.CPP

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlProperty>
#include <QQmlComponent>
#include <QDebug>
#include "myclass.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);   
    QQmlApplicationEngine engine;
    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *object = component.create();
        MyClass myClass;
        QObject::connect(object, SIGNAL(qmlSignal(QVariant)),
                         &myClass, SLOT(cppSlot(QVariant)));

    return app.exec();
}

我的myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QString>
#include <QList>

class MyClass : public QObject
{
    Q_OBJECT
public:
    MyClass(QObject *parent = 0);
signals:
public slots:
    void cppSlot(QVariant &msg);
};
#endif // MYCLASS_H

我的myclass.cpp

#include "myclass.h"
MyClass::MyClass(QObject *parent):
    QObject(parent)
{
}
void MyClass::cppSlot(QVariant &msg){
    qDebug() << "c++: bien reçu chef " << msg;
}

我不明白为什么我不能在此信号中放置QVariant参数。欢迎任何帮助:)

更改插槽以按值而不是参考接收其参数。这样:

    void cppSlot(QVariant msg);

我认为这与QML拒绝通过参考传递Window的属性有关。无论如何,即使您的信号/插槽签名[s]声明参数 by-referference ,QT通常会发送信号 by-value 。有关该主题的更多信息,请参阅此内容

不要使用qml和c 之间的参考,它们不起作用。

void cppslot(qvariant &amp; msg);

您还可以在C 中创建q_invokable函数,并直接从QML调用。这是这样:

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlProperty>
#include <QQmlComponent>
#include <QDebug>
#include "myclass.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);   
    QQmlApplicationEngine engine;
    auto myobject = new MyClass(&app);
    engine.rootContext()->setContextProperty(QStringLiteral("myobject"), myobject);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
    {
        return -1;
    }
    return app.exec();
}

mycass.h:

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QString>
#include <QList>
class MyClass : public QObject
{
    Q_OBJECT
public:
    MyClass(QObject *parent = 0);
    Q_INVOKABLE void cppSlot(QVariant msg);
};
#endif // MYCLASS_H

myClass.cpp:

#include "myclass.h"
MyClass::MyClass(QObject *parent):QObject(parent){}
void MyClass::cppSlot(QVariant msg)
{
    qDebug() << "c++: bien reçu chef " << msg;
}

main.qml:

import QtQuick 2.4
import QtQuick.Layouts 1.1
import Material.ListItems 0.1 as ListItem
import Material.Extras 0.1
import QtQuick.Controls 1.3 as QuickControls
import QtQuick.Controls 1.4
import Material 0.2
Window 
{
    visible: true
    property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
    id: item
    width: 100; height: 100
    Rectangle
    {
        id:sousRect
        color:"red"
        width:100; height:100
        Button
        {
            id:buttonTest
            onClicked: myobject.cppSlot(listCloud)
        }
    }
} 

但是,如果您想使用信号插槽,则QML中有连接对象。您的主QML将看起来像这样:

Window 
{
    visible: true
    property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
    id: item
    width: 100; height: 100
    Connections
    {
        target: buttonTest
        onClicked: myobject.cppSlot(listCloud)
    }
    Rectangle
    {
        id:sousRect
        color:"red"
        width:100; height:100
        Button
        {
            id:buttonTest
        }
    }
}

q_invokable别无其他作为公共插槽,它将通过meta-object系统来调用。