簡體   English   中英

將QVariant從QML傳遞到C ++

[英]Passing QVariant from QML to C++

我正在嘗試將列表從QML發送到c ++。 我嘗試使用成功的字符串和整數,但是當我嘗試使用QVariant時出現錯誤:

QObject :: connect:../test_2206/main.cpp:31中沒有這樣的插槽MyClass :: cppSlot(QVariant)

我的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的屬性有關。 無論如何,即使您的信號/插槽簽名聲明參數by-reference ,Qt也經常發送信號by-value 欲了解更多的話題看到這個那個

不要在QML和C ++之間使用引用,它們將無法工作。

無效cppSlot(QVariant 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)
        }
    }
} 

但是,如果您想使用SIGNAL SLOTS,則QML中有Connections對象。 您的main.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作為公共SLOT別無其他,它將通過元對象系統調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM