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
    width: 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.width: 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.width: 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
        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: 250
                    easing.type: Easing.InOutQuad
                }
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/ike_li/p/10436779.html

1. model:指定ListView的数据源,可以是一个ListModel、一个数组、一个Json对象、一个数据库查询等等。 2. delegate:指定ListView中每个元素的外观,可以是一个Item,也可以是自定义的组件。 3. width和height:指定ListView的宽度和高度。 4. spacing:指定ListView中每个元素之间的间距。 5. orientation:指定ListView的方向,可以是横向或纵向。 6. clip:当ListView的内容超出边界时,指定是否裁剪超出部分。 7. highlight:指定ListView中选中元素的外观,可以是一个Item,也可以是自定义的组件。 8. currentIndex:指定当前选中元素的索引。 9. focus:指定ListView是否可以获取焦点。 10. interactive:指定ListView是否可以交互。 11. moving:指定ListView是否可以移动。 12. snapMode:指定ListView滚动时的对齐方式,可以是一个值或数组。 13. highlightFollowsCurrentItem:指定ListView中选中元素的高亮是否跟随当前元素移动。 14. preferredHighlightBegin:指定ListView中选中元素高亮的起始位置。 15. preferredHighlightEnd:指定ListView中选中元素高亮的结束位置。 16. keys:指定ListView中用于快速定位元素的键。 17. section.property:指定ListView中按照属性分组的方式,可以在delegate中使用section来访问分组数据。 18. header:指定ListView中每个分组的头部元素。 19. footer:指定ListView中每个分组的尾部元素。 20. highlightMoveDuration:指定选中元素移动到中间位置的动画时长。 21. highlightMoveVelocity:指定选中元素移动到中间位置的动画速度。 22. highlightRangeMode:指定选中元素高亮的范围,可以是一个值或数组。 23. preferredHighlightWidth:指定选中元素高亮的宽度。 24. preferredHighlightHeight:指定选中元素高亮的高度。 25. preferredHighlightColor:指定选中元素高亮的颜色。 26. preferredHighlightBackgroundColor:指定选中元素高亮的背景颜色。 27. highlightFollowsCurrentItem:指定选中元素高亮是否跟随当前元素移动。 28. interactive:指定ListView是否可以交互。 29. moving:指定ListView是否可以移动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值