add book
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(7-DynamicModels VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick QuickControls2)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app7-DynamicModels
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app7-DynamicModels
|
||||
URI 7-DynamicModels
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml
|
||||
)
|
||||
|
||||
set_target_properties(app7-DynamicModels PROPERTIES
|
||||
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
|
||||
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
|
||||
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
|
||||
MACOSX_BUNDLE TRUE
|
||||
WIN32_EXECUTABLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(app7-DynamicModels
|
||||
PRIVATE Qt6::Quick Qt6::QuickControls2
|
||||
)
|
||||
|
||||
install(TARGETS app7-DynamicModels
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
import QtQuick.Window
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Dynamic Models Demo")
|
||||
|
||||
ListModel {
|
||||
id: mListModel
|
||||
|
||||
ListElement {
|
||||
firstName: "John"; lastName: "Snow"
|
||||
}
|
||||
ListElement {
|
||||
firstName: "Nicholai"; lastName: "Itchenko"
|
||||
}
|
||||
ListElement {
|
||||
firstName: "Mitch"; lastName: "Mathson"
|
||||
}
|
||||
ListElement {
|
||||
firstName: "Ken"; lastName: "Kologorov"
|
||||
}
|
||||
ListElement {
|
||||
firstName: "Vince"; lastName: "Luvkyj"
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
ListView{
|
||||
id: mListViewId
|
||||
model: mListModel
|
||||
delegate: delegateId
|
||||
Layout.fillWidth : true
|
||||
Layout.fillHeight: true
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Button {
|
||||
text: "Add Item"
|
||||
Layout.fillWidth : true
|
||||
onClicked: {
|
||||
mListModel.append({"firstName": "Daniel", "lastName": "Gakwaya"})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Button {
|
||||
text: "Clear"
|
||||
Layout.fillWidth : true
|
||||
onClicked: {
|
||||
mListModel.clear()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Button {
|
||||
text: "Delete Item at index 2"
|
||||
Layout.fillWidth: true
|
||||
onClicked: {
|
||||
if ( 2 < mListViewId.model.count){
|
||||
mListModel.remove(2,1)
|
||||
}else{
|
||||
console.log("index is invalid")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Button {
|
||||
text: "Set item at index 1"
|
||||
Layout.fillWidth: true
|
||||
onClicked: {
|
||||
mListModel.set(1,{"firstName": "John", "lastName": "Doe"})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Component {
|
||||
id: delegateId
|
||||
Rectangle {
|
||||
id: rectangleId
|
||||
width: mListViewId.width
|
||||
height: 50
|
||||
color: "beige"
|
||||
border.color: "yellowgreen"
|
||||
radius: 14
|
||||
|
||||
Text {
|
||||
id: textId
|
||||
anchors.centerIn: parent
|
||||
text : firstName + " " + lastName
|
||||
font.pointSize: 20
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: {
|
||||
console.log("Clicked on: " + firstName + " " + lastName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQuickStyle>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QQuickStyle::setStyle("Material");
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("7-DynamicModels", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user