QML学习笔记3(qml注册有参单例)

使用匿名函数注册单例

SQLiteHelper *sqliteHelper=new  SQLiteHelper();
    qmlRegisterSingletonType<PersonListViewModel>("MyPackage",1,0,"PersonListViewModel",[=](QQmlEngine *engine, QJSEngine *scriptEngine){
        Q_UNUSED(engine);
        Q_UNUSED(scriptEngine)
        PersonListViewModel *personListViewModel=new PersonListViewModel(sqliteHelper);
        return  personListViewModel;
    });

qml使用单例

import MyPackage 1.0 as MyPackage10
model: MyPackage10.PersonListViewModel
import QtQuick 2.12
import QtQuick.Controls 2.5
import MyPackage 1.0 as MyPackage10

Rectangle {
    anchors.fill: parent
    anchors.leftMargin: 3
    anchors.rightMargin: 3
    anchors.topMargin: 3
    anchors.bottomMargin: 3
    property var _ID
    property var _Name
    property var _Age
    property int _Index: -1
    color: "transparent"
    Rectangle{
        id:header
        height: 50
        width: parent.width
        Grid {
            id: grid
            columns: 4
            rows: 2
            width: parent.width

            Label{
                text: "姓名:"


            }
            TextInput {
                id:personname
                text: _Name===undefined?"未选择":_Name
                color: "blue"
            }
            Label{
                text: "年龄:"
                color: "red"
            }
            TextInput {
                id:personage
                text: _Age===undefined?"未选择":_Age
                color: "blue"
            }
            Button{
                text: "修改"
                onClicked: {
                    MyPackage10.PersonListViewModel.updataPerson(_ID,personname.text,personage.text)
                }
            }
            Button{
                text: "添加"
                onClicked: {
                    console.log("姓名:"+personname.text+"年龄"+personage.text)
                    MyPackage10.PersonListViewModel.addItem(personname.text,personage.text);
                }
            }
            Button{
                text: "删除"
                onClicked: {
                    MyPackage10.PersonListViewModel.deleteItem(_ID);
                    list.currentIndex=-1;
                }
            }
        }
    }
    ListView {
        id:list
        width: parent.width;
        anchors.top: header.bottom
        anchors.bottom: parent.bottom
        model: MyPackage10.PersonListViewModel
        spacing: 3
        clip: true
        highlight: Rectangle{
            radius: 10
            width: parent===null?0:parent.width
            color: "blue"
        }
        ScrollBar.vertical: ScrollBar{}
        highlightFollowsCurrentItem: true
        keyNavigationEnabled:true
        delegate:Rectangle {
            radius: 10
            border.color: "gray"
            color: "transparent"
            height: 30
            width: parent.width
            anchors.leftMargin: 5
            Text {
                x:10
                anchors.fill: parent
                anchors.leftMargin: 5
                text:"姓名:"+name+"----------年龄:"+age+"----------ID:"+id
                verticalAlignment:Text.AlignVCenter
            }
            MouseArea{
                anchors.fill: parent
                onClicked: {

                    _Age=age
                    _Name=name
                    _ID=id
                    _Index=index;
                    list.currentIndex=_Index;
                    console.log(_Name,_Age,_ID)

                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/dwm88888888/article/details/131327605
QML