add book
This commit is contained in:
40
Qt6QMLBeginnersCode/6.Javascript/2-JsUsage/CMakeLists.txt
Normal file
40
Qt6QMLBeginnersCode/6.Javascript/2-JsUsage/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(2-JsUsage VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app2-JsUsage
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app2-JsUsage
|
||||
URI 2-JsUsage
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml
|
||||
)
|
||||
|
||||
set_target_properties(app2-JsUsage 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(app2-JsUsage
|
||||
PRIVATE Qt6::Quick
|
||||
)
|
||||
|
||||
install(TARGETS app2-JsUsage
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
38
Qt6QMLBeginnersCode/6.Javascript/2-JsUsage/Main.qml
Normal file
38
Qt6QMLBeginnersCode/6.Javascript/2-JsUsage/Main.qml
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("JS")
|
||||
|
||||
Rectangle {
|
||||
id: containerRectId
|
||||
width: getHeight() //JS in Function
|
||||
height: 100
|
||||
color: x > 300 ? "red" : "green" //property binding
|
||||
|
||||
//JS in signal handler
|
||||
onXChanged: {
|
||||
console.log("Current value of x: "+ x)
|
||||
}
|
||||
|
||||
//Custom function
|
||||
function getHeight()
|
||||
{
|
||||
return height * 2
|
||||
}
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
drag.target: containerRectId
|
||||
drag.axis: Drag.XAxis
|
||||
drag.minimumX: 0
|
||||
drag.maximumX: parent.width - containerRectId.width
|
||||
|
||||
}
|
||||
}
|
||||
20
Qt6QMLBeginnersCode/6.Javascript/2-JsUsage/main.cpp
Normal file
20
Qt6QMLBeginnersCode/6.Javascript/2-JsUsage/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("2-JsUsage", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(3-FunctionsAndScope VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app3-FunctionsAndScope
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app3-FunctionsAndScope
|
||||
URI 3-FunctionsAndScope
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml
|
||||
)
|
||||
|
||||
set_target_properties(app3-FunctionsAndScope 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(app3-FunctionsAndScope
|
||||
PRIVATE Qt6::Quick
|
||||
)
|
||||
|
||||
install(TARGETS app3-FunctionsAndScope
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Functions and Scope Demo")
|
||||
|
||||
function min ( a ,b)
|
||||
{
|
||||
return Math.min(a,b)
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
id: mRectId
|
||||
width: min( 500,400)
|
||||
height: 100
|
||||
anchors.centerIn: parent
|
||||
color: "blue"
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: mMouseAreaId
|
||||
anchors.fill: parent
|
||||
|
||||
function sayMessage()
|
||||
{
|
||||
console.log("Hello there")
|
||||
}
|
||||
|
||||
onClicked: {
|
||||
sayMessage()
|
||||
console.log(min(10,12))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
console.log("The width of the rect is: "+ min(500,400))
|
||||
mMouseAreaId.sayMessage()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("3-FunctionsAndScope", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(4-JsDirectImpoart VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app4-JsDirectImpoart
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app4-JsDirectImpoart
|
||||
URI 4-JsDirectImpoart
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml utilities1.js
|
||||
)
|
||||
|
||||
set_target_properties(app4-JsDirectImpoart 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(app4-JsDirectImpoart
|
||||
PRIVATE Qt6::Quick
|
||||
)
|
||||
|
||||
install(TARGETS app4-JsDirectImpoart
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
33
Qt6QMLBeginnersCode/6.Javascript/4-JsDirectImpoart/Main.qml
Normal file
33
Qt6QMLBeginnersCode/6.Javascript/4-JsDirectImpoart/Main.qml
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
import "utilities1.js" as Utilities1
|
||||
|
||||
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Javascript Direct Import")
|
||||
|
||||
Rectangle {
|
||||
width: 300
|
||||
height: 100
|
||||
color: "yellowgreen"
|
||||
anchors.centerIn: parent
|
||||
|
||||
Text {
|
||||
text: "Click Me"
|
||||
anchors.centerIn: parent
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: {
|
||||
//console.log("Hello there")
|
||||
Utilities1.greeting()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Qt6QMLBeginnersCode/6.Javascript/4-JsDirectImpoart/main.cpp
Normal file
20
Qt6QMLBeginnersCode/6.Javascript/4-JsDirectImpoart/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("4-JsDirectImpoart", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
function greeting()
|
||||
{
|
||||
console.log("Hello there from external JS file utilities1.js")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(5-JsIndirectImprt VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app5-JsIndirectImprt
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app5-JsIndirectImprt
|
||||
URI 5-JsIndirectImprt
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml utilities1.js utilities2.js
|
||||
)
|
||||
|
||||
set_target_properties(app5-JsIndirectImprt 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(app5-JsIndirectImprt
|
||||
PRIVATE Qt6::Quick
|
||||
)
|
||||
|
||||
install(TARGETS app5-JsIndirectImprt
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
32
Qt6QMLBeginnersCode/6.Javascript/5-JsIndirectImprt/Main.qml
Normal file
32
Qt6QMLBeginnersCode/6.Javascript/5-JsIndirectImprt/Main.qml
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
import "utilities1.js" as Utilities1
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Javascript Import Demo")
|
||||
|
||||
Rectangle {
|
||||
width: 300
|
||||
height: 100
|
||||
color: "yellowgreen"
|
||||
anchors.centerIn: parent
|
||||
|
||||
Text {
|
||||
text: "Click Me"
|
||||
anchors.centerIn: parent
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: {
|
||||
//console.log("The ages yield: " + Utilities1.combineAges(33,17))
|
||||
value = Utilities1.add(33,17) //Error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
Qt6QMLBeginnersCode/6.Javascript/5-JsIndirectImprt/main.cpp
Normal file
20
Qt6QMLBeginnersCode/6.Javascript/5-JsIndirectImprt/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("5-JsIndirectImprt", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
.import "utilities2.js" as Utilities2
|
||||
|
||||
function greeting()
|
||||
{
|
||||
console.log("Hello there from external JS file: utilities1.js")
|
||||
}
|
||||
|
||||
function combineAges( age1, age2)
|
||||
{
|
||||
return Utilities2.add(age1,age2)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
function add(a,b) {
|
||||
console.log("Method from utilities2.js called")
|
||||
return a + b
|
||||
}
|
||||
40
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/CMakeLists.txt
Normal file
40
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(6-QtInclude VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app6-QtInclude
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app6-QtInclude
|
||||
URI 6-QtInclude
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml utilities1.js utilities2.js
|
||||
)
|
||||
|
||||
set_target_properties(app6-QtInclude 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(app6-QtInclude
|
||||
PRIVATE Qt6::Quick
|
||||
)
|
||||
|
||||
install(TARGETS app6-QtInclude
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
33
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/Main.qml
Normal file
33
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/Main.qml
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
import "utilities1.js" as Utilities1
|
||||
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Javascript Import Demo")
|
||||
|
||||
Rectangle {
|
||||
width: 300
|
||||
height: 100
|
||||
color: "yellowgreen"
|
||||
anchors.centerIn: parent
|
||||
|
||||
Text {
|
||||
text: "Click Me"
|
||||
anchors.centerIn: parent
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: {
|
||||
//console.log("The ages yield: " + Utilities1.combineAges(33,17))
|
||||
console.log("The ages yield: " + Utilities1.add(33,17))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/main.cpp
Normal file
20
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("6-QtInclude", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
14
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/utilities1.js
Normal file
14
Qt6QMLBeginnersCode/6.Javascript/6-QtInclude/utilities1.js
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
Qt.include("utilities2.js")
|
||||
|
||||
function greeting()
|
||||
{
|
||||
console.log("Hello there from external JS file: utilities1.js")
|
||||
}
|
||||
|
||||
function combineAges( age1, age2)
|
||||
{
|
||||
return add(age1,age2)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
function add(a,b) {
|
||||
console.log("Method from utilities2.js called")
|
||||
return a + b
|
||||
}
|
||||
40
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/CMakeLists.txt
Normal file
40
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(7-JsModules VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app7-JsModules
|
||||
main.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app7-JsModules
|
||||
URI 7-JsModules
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml utilities1.mjs utilities2.mjs
|
||||
)
|
||||
|
||||
set_target_properties(app7-JsModules 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-JsModules
|
||||
PRIVATE Qt6::Quick
|
||||
)
|
||||
|
||||
install(TARGETS app7-JsModules
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
41
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/Main.qml
Normal file
41
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/Main.qml
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
import "utilities1.mjs" as Utilities1
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Javascript Import Demo")
|
||||
|
||||
Rectangle {
|
||||
|
||||
width: 300
|
||||
height: 100
|
||||
color: "yellowgreen"
|
||||
anchors.centerIn: parent
|
||||
Text {
|
||||
text: "Click Me"
|
||||
anchors.centerIn: parent
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: {
|
||||
//Calling a properly exported mathod
|
||||
/*
|
||||
console.log("The ages yield: " + Utilities1.combineAges(33,17))
|
||||
*/
|
||||
|
||||
|
||||
//Can't call a method that's not exported: subtract isn't exported from utilities2.mjs
|
||||
console.log("Age diff: " + Utilities1.ageDiff(33,17))
|
||||
|
||||
//Just because add is usable from utilities1.js doesn't mean main.qml
|
||||
// can use it. utilities2.js doesn't export it.
|
||||
//console.log ("The sum is: " + Utilities1.add(33,17))//Error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/main.cpp
Normal file
20
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("7-JsModules", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
12
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/utilities1.mjs
Normal file
12
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/utilities1.mjs
Normal file
@@ -0,0 +1,12 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import * as Utilities2 from "utilities2.mjs";
|
||||
|
||||
export function combineAges(age1,age2){
|
||||
return Utilities2.add(age1,age2)
|
||||
}
|
||||
|
||||
export function ageDiff(age1,age2){
|
||||
return Utilities2.subtract(age1,age2)
|
||||
}
|
||||
10
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/utilities2.mjs
Normal file
10
Qt6QMLBeginnersCode/6.Javascript/7-JsModules/utilities2.mjs
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
export function add(age1,age2){
|
||||
return age1 + age2
|
||||
}
|
||||
|
||||
function subtract(age1,age2){
|
||||
return age1 - age2;
|
||||
}
|
||||
Reference in New Issue
Block a user