C
Qt Quick Ultralite 列表模型示例
/****************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/import QtQuick 2.15 import QtQuick.Controls 2.15 Rectangle { id: root color: "#ccc"; // Create an instance of the custom AlarmModel AlarmModel { id: alarmModel } // Call AlarmModel::tick() every second. Timer { running: true repeat: true interval: 1000 onTriggered: alarmModel.tick() } // Declare the delegate that the ListView (below) instantiates for each // entry in the alarm model. // // It shows information about the entry and two buttons, one to pause or // unpause the alarm, and one to remove it from the list. Component { id: alarmDelegate Item { width: root.width height: 60 Text { x: 10 anchors.verticalCenter: parent.verticalCenter text: { // This function computes what text to show for the entry. // It uses model.seconds to access the "seconds" field // of the AlarmData associated with this delegate instance. if (model.seconds >= 0) return "in " + model.seconds + " seconds" return (-model.seconds) + " seconds ago" } color: { if (model.seconds >= 0) { if (model.running) return "green" return "red" } return "gray" } } Row { anchors.right: parent.right Button { text: model.running ? "Pause" : "Unpause" visible: model.seconds > 0 // The "index" property contains the model index for // the current delegate instance. onClicked: alarmModel.togglePause(index) } Button { text: "Remove" onClicked: alarmModel.remove(index) } } } } // This ListView is the main display element. It creates an alarmDelegate // instance for each entry in alarmModel and listens to alarmModel changes. ListView { width: parent.width anchors.top: parent.top anchors.bottom: controls.top model: alarmModel delegate: alarmDelegate } Rectangle { id: controls color: "#aae" height: createNewButton.height width: parent.width anchors.bottom: parent.bottom Button { id: createNewButton text: "Create new" anchors.right: parent.right onClicked: alarmModel.addEntry(15) } } }