
正文
QML ListView json
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickItem>
#include <QVariant>
#include <QDebug>
#include "jsondata.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); TestData *testData=new TestData(); QQmlApplicationEngine engine;
JsonData jsondata;
engine.rootContext()->setContextProperty("jsondata",&jsondata); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -; //QQuickItem* item = engine.rootObjects().at(0)->findChild<QQuickItem*>("tableview");
//qDebug() << item->objectName();
//QObject::connect (testData,SIGNAL (sendToQml(QVariant)),item,SLOT(addRowData(QVariant))); //QString jsonData="[{\"task_name\":\"task1\",\"task_status\":\"ok\",\"task_time\":\"2019_2_16\"}]";
//QMetaObject::invokeMethod(item, "addRowData", Q_ARG(QVariant, jsonData)); //调用函数 return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Window 2.2
import QtQuick.VirtualKeyboard 2.2 Window {
id: window
visible: true
width:
height:
title: qsTr("Forklift") property var jsonData: JSON.parse(jsondata.getJsonData)
readonly property color bgColor: "#40434A"
readonly property color borderColor: "#6affcd"
readonly property color colorLightGrey: "#888"
color: bgColor Rectangle{
id: container
anchors.fill: parent
anchors.margins:
border.color:borderColor
border.width:
color: bgColor
smooth: true
RowLayout{
id: mainRowLayout
anchors.fill: parent
anchors.margins:
spacing: Rectangle{
id:leftRect
Layout.fillHeight: true
Layout.minimumWidth:
Layout.preferredWidth:
Layout.maximumWidth:
Layout.minimumHeight:
border.color: borderColor
color: bgColor
border.width:
smooth: true // TableViewItem{
// id:taskTableView
// anchors.fill: parent
// objectName: "tableview" // } ListView{
id:taskListview
anchors.fill: parent
anchors.margins:
model: jsonData
delegate: tableModel }
Component{
id:tableModel
RowLayout{
spacing:
Layout.fillWidth:true
Label{
text: jsonData[index].name
color:borderColor
}
Label{
text: jsonData[index].age
color:borderColor
}
}
} } Rectangle {
color: borderColor
implicitWidth:
Layout.fillHeight: true
} Rectangle{
id:centerRect
Layout.fillHeight: true
Layout.minimumWidth:
Layout.preferredWidth:
Layout.maximumWidth:
Layout.minimumHeight:
color: bgColor
Label{
text: qsTr("01/01/2018")
color: colorLightGrey
font.pixelSize: Layout.alignment: Qt.AlignHCenter
Layout.topMargin:
Layout.bottomMargin:
} ColumnLayout{
Layout.preferredWidth:
Layout.fillWidth: true
Layout.fillHeight: true
Image{
source: "qrc:/icons/car.png"
fillMode: Image.PreserveAspectFit
Layout.fillHeight: true
}
} } Rectangle {
color: borderColor
implicitWidth:
Layout.fillHeight: true
} Rectangle{
id:rightRect
Layout.fillHeight: true
Layout.minimumWidth:
Layout.preferredWidth:
Layout.maximumWidth:
Layout.minimumHeight: } } } InputPanel {
id: inputPanel
z:
x:
y: window.height
width: window.width states: State {
name: "visible"
when: inputPanel.active
PropertyChanges {
target: inputPanel
y: window.height - inputPanel.height
}
}
transitions: Transition {
from: ""
to: "visible"
reversible: true
ParallelAnimation {
NumberAnimation {
properties: "y"
duration:
easing.type: Easing.InOutQuad
}
}
}
}
}








