我们都知道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")
}
}
运行结果: