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 -1;

    //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
     1000
    height: 600
    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: 20
        border.color:borderColor
        border. 1
        color: bgColor
        smooth: true
        RowLayout{
           id: mainRowLayout
           anchors.fill: parent
           anchors.margins: 24
           spacing: 36

            Rectangle{
                id:leftRect
                Layout.fillHeight: true
                Layout.minimumWidth: 200
                Layout.preferredWidth: 300
                Layout.maximumWidth: 300
                Layout.minimumHeight: 150
                border.color: borderColor
                color: bgColor
                border. 1
                smooth: true

//                TableViewItem{
//                 id:taskTableView
//                 anchors.fill: parent
//                 objectName: "tableview"

//                }

                ListView{
                    id:taskListview
                    anchors.fill: parent
                    anchors.margins: 10
                    model: jsonData
                    delegate: tableModel

                }
                Component{
                    id:tableModel
                    RowLayout{
                        spacing:  10
                        Layout.fillWidth:true
                        Label{
                            text: jsonData[index].name
                            color:borderColor
                        }
                        Label{
                            text: jsonData[index].age
                            color:borderColor
                        }
                    }
                }

            }


           Rectangle {
             color: borderColor
             implicitWidth: 1
             Layout.fillHeight: true
           }

           Rectangle{
               id:centerRect
               Layout.fillHeight: true
               Layout.minimumWidth: 200
               Layout.preferredWidth: 100
               Layout.maximumWidth: 300
               Layout.minimumHeight: 150
               color: bgColor
               Label{
                   text: qsTr("01/01/2018")
                   color: colorLightGrey
                   font.pixelSize: 12

                   Layout.alignment: Qt.AlignHCenter
                   Layout.topMargin: 2
                   Layout.bottomMargin: 10
               }

               ColumnLayout{
                   Layout.preferredWidth: 350
                   Layout.fillWidth: true
                   Layout.fillHeight: true
                   Image{
                       source: "qrc:/icons/car.png"
                       fillMode: Image.PreserveAspectFit
                       Layout.fillHeight: true
                   }
               }

           }


           Rectangle {
             color: borderColor
             implicitWidth: 1
             Layout.fillHeight: true
           }

           Rectangle{
               id:rightRect
               Layout.fillHeight: true
               Layout.minimumWidth: 200
               Layout.preferredWidth: 100
               Layout.maximumWidth: 300
               Layout.minimumHeight: 150

           }


        }








    }


    InputPanel {
        id: inputPanel
        z: 99
        x: 0
        y: window.height
         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: 250
                    easing.type: Easing.InOutQuad
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/ike_li/p/10436779.html