qml向Qt的C++传数组参数

我们都知道qml和C++可以进行交互,但数据交互的过程中,一般只限于内置类型,比如int, bool,double等,如果是json或数组由C++传给qml,qml是可以解析的,但如果是qml传数组给C++呢,该如何传参及解析数组参数呢,C++中通过QVariantList来进处理参数处理,qml参数封装如下:

var infoArray = [];//先清空
infoArray.push("productVersion");
infoArray.push("productType");
infoArray.push("kernelType");
infoArray.push("currentCpuArchitecture");

C++解析:

QJsonObject SystemService::getSystemInfo(const QVariantList &list)
{
    for(int i = 0; i < list.count(); ++i)
    {
        QString typeName = list[i].toString();
  
    }

    return jsonObject;
}

完整的示例代码如下:

#ifndef SYSTEMSERVICE_H
#define SYSTEMSERVICE_H

#include <QObject>
#include <QSysInfo>
#include <QJsonObject>
#include <QVariantList>

class SystemService: public QObject
{
    Q_OBJECT

public:
    SystemService(QObject *parent = nullptr);

    Q_INVOKABLE QJsonObject getSystemInfo(const QVariantList &list);
    Q_INVOKABLE QString getSystemName();
};

#endif // SYSTEMSERVICE_H
#include "systemservice.h"


SystemService::SystemService(QObject *parent)
    :QObject(parent)
{

}

QJsonObject SystemService::getSystemInfo(const QVariantList &list)
{
    QString strProductVersion = QSysInfo::productVersion();
    QString strProductType = QSysInfo::productType();
    QString strKernelType = QSysInfo::kernelType();
    QString strCurrentCpuArchitecture = QSysInfo::currentCpuArchitecture();
    QJsonObject jsonObject;
    for(int i = 0; i < list.count(); ++i)
    {
        QString typeName = list[i].toString();
        if("productVersion" == typeName)
            jsonObject.insert("productVersion", strProductVersion);
        else if("productType" == typeName)
            jsonObject.insert("productType", strProductType);
        else if("kernelType" == typeName)
            jsonObject.insert("kernelType", strKernelType);
        else if("currentCpuArchitecture" == typeName)
            jsonObject.insert("currentCpuArchitecture", strCurrentCpuArchitecture);
    }

    return jsonObject;
}

QString SystemService::getSystemName()
{
    return QSysInfo::productType();
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "systemservice.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    qmlRegisterType<SystemService>("CPPService", 1, 0, "SystemService");

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import CPPService 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    SystemService {
        id: systemService
    }

    Button {
        text: qsTr("获取信息")
        onClicked: {
            var infoArray = [];//先清空
            infoArray.push("productVersion");
            infoArray.push("productType");
            infoArray.push("kernelType");
            infoArray.push("currentCpuArchitecture");
            console.log("infoArray=======================" + infoArray)
            var systemInfo = systemService.getSystemInfo(infoArray)
            //arrayText.text = JSON.stringify(systemInfo)
            arrayText.text = systemInfo["productVersion"] + "\n" + systemInfo["productType"] + "\n" + systemInfo["kernelType"] + "\n" + systemInfo["currentCpuArchitecture"] + "\n"
            var type = systemService.getSystemName()
            typeText.text = type
            console.log("type=======================" + type)
        }
    }

    Text {
        id: arrayText
        color: "blue"
        anchors.top: parent.top
        anchors.topMargin: 100
        anchors.left: parent.left
        anchors.leftMargin: 20
        text: qsTr("text")
    }
    Text {
        id: typeText
        color: "red"
        anchors.top: parent.top
        anchors.topMargin: 100
        anchors.right: parent.right
        anchors.rightMargin: 20
        text: qsTr("type")
    }
}

运行结果:

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值