Skip to content

Commit

Permalink
Correct qml5-contextproperty
Browse files Browse the repository at this point in the history
  • Loading branch information
herzenschein committed Jun 9, 2024
1 parent e69eafe commit 2a5c4ec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
28 changes: 23 additions & 5 deletions qml5-contextproperty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.23)

# First argument will be PROJECT_NAME
project(qml6-contextproperty LANGUAGES CXX)
project(qml5-contextproperty LANGUAGES CXX)

# We need to find Qt before we can use
# the new Qt CMake API
Expand All @@ -15,8 +15,11 @@ qt_standard_project_setup()
qt_policy(SET QTP0001 NEW)

# Create a target executable
qt_add_executable(${PROJECT_NAME}
main.cpp
qt_add_executable(${PROJECT_NAME})

target_sources(${PROJECT_NAME}
PRIVATE
"main.cpp"
)

# This automatically creates resources,
Expand All @@ -31,8 +34,23 @@ qt_add_qml_module(${PROJECT_NAME}
QML_FILES
"Main.qml"
SOURCES
"MyFunctionsFromCPPToQml.hpp"
"MyFunctionsFromCppToQml.hpp"
)

# Finish by linking the necessary Qt libraries
target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Quick)
target_link_libraries(${PROJECT_NAME}
PRIVATE
Qt::Quick
)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_qml_app_script(
TARGET ${PROJECT_NAME}
OUTPUT_SCRIPT deployScript
)

install(SCRIPT ${deployScript})
19 changes: 11 additions & 8 deletions qml5-contextproperty/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ApplicationWindow {
title: _windowtitle
visible: true

width: 500
height: 500

Material.theme: Material.Dark

ColumnLayout {
Expand All @@ -17,18 +20,18 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignCenter

text: MyCPPFunctions.normalString()
text: MyCppFunctions.normalString()
onClicked: {
MyCPPFunctions.normalMethod()
MyCppFunctions.normalMethod()
}
}

Button {
Layout.alignment: Qt.AlignCenter

text: MyCPPFunctions.publicString()
text: MyCppFunctions.publicString()
onClicked: {
MyCPPFunctions.publicSlot()
MyCppFunctions.publicSlot()
}
}

Expand All @@ -37,15 +40,15 @@ ApplicationWindow {

text: {
// JS ternary operator
MyCPPFunctions.privateString
? MyCPPFunctions.privateString()
MyCppFunctions.privateString
? MyCppFunctions.privateString()
: "This private string is inaccessible"
}

onClicked: {
// JS ternary operator
MyCPPFunctions.privateSlot
? MyCPPFunctions.privateSlot()
MyCppFunctions.privateSlot
? MyCppFunctions.privateSlot()
: console.info("This private slot is inaccessible")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <QDebug>
#include <QString>

class MyFunctionsFromCPPToQml : public QObject
class MyFunctionsFromCppToQml : public QObject
{
Q_OBJECT

Expand Down
6 changes: 3 additions & 3 deletions qml5-contextproperty/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "MyFunctionsFromCPPToQml.hpp"
#include "MyFunctionsFromCppToQml.hpp"

int main(int argCounter, char* argVector[])
{
QGuiApplication contextPropertyApp(argCounter, argVector);

MyFunctionsFromCPPToQml myFunctionsInstance;
MyFunctionsFromCppToQml myFunctionsInstance;

QQmlApplicationEngine engine;

// We can arbitrarily export individual properties globally.
// By convention, it starts with an underscore.
engine.rootContext()->setContextProperty("_windowtitle", QString("Minimal QML6 Context Property Example"));
// Or we can export entire types and their respective properties.
engine.rootContext()->setContextProperty("MyCPPFunctions", &myFunctionsInstance);
engine.rootContext()->setContextProperty("MyCppFunctions", &myFunctionsInstance);

// This method simply loads the file and is unnecessarily verbose:
// engine.load("qrc:/qt/qml/com/example/contextproperty/Main.qml");
Expand Down

0 comments on commit 2a5c4ec

Please sign in to comment.