From 558fba07a6ea41e01b11cb98ada86ca87b75c828 Mon Sep 17 00:00:00 2001 From: Tal Lancaster Date: Wed, 18 Jan 2017 15:53:29 -0700 Subject: [PATCH] mp2.cpp: new example on getting properties hw.qml: findChild uses objectName; so added it. --- mp/CMakeLists.txt | 7 ++++++- mp/mp2.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ qml/hw.qml | 1 + 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 mp/mp2.cpp diff --git a/mp/CMakeLists.txt b/mp/CMakeLists.txt index 9a15a32..5f8f9ee 100644 --- a/mp/CMakeLists.txt +++ b/mp/CMakeLists.txt @@ -1,7 +1,8 @@ +cmake_minimum_required(VERSION 3.0) + # sets the PROJECT_NAME variable project(mpApp) -cmake_minimum_required(VERSION 3.0) set(CMAKE_AUTOMOC ON) @@ -15,3 +16,7 @@ add_executable(${PROJECT_NAME} ${SRC_LIST}) # links Qt5Core to the project executable target_link_libraries(${PROJECT_NAME} Qt5::Qml Qt5::Quick) +set (SRC2_LIST mp2.cpp) +add_executable(mp2App ${SRC2_LIST}) +target_link_libraries(mp2App Qt5::Qml Qt5::Quick) + diff --git a/mp/mp2.cpp b/mp/mp2.cpp new file mode 100644 index 0000000..35dc1e7 --- /dev/null +++ b/mp/mp2.cpp @@ -0,0 +1,43 @@ +/*** + Simple example showing how to read a property. + Specifically getting the text property from a Text object. + and converting it to std::string. + + NOTE: The findChild searches on objectName and not id. + id is internal to Quick. + + + 1/17/17 -- Tal Lancaster + ***/ + +#include +#include + +#include +#include +#include +#include +#include + +int main (int argc, char* argv[]) +{ + + QGuiApplication q_app (argc, argv); + + // Using QQuickView + QQuickView view; + view.setSource(QUrl::fromLocalFile("../qml/hw.qml")); + view.show(); + QObject* object {view.rootObject () }; + QObject* text {object->findChild ("helloText") }; + if (text) { + std::string str {text->property ("text").toString().toStdString() }; + std::cout << "from text obj " << str << "\n"; + } + else { + std::cout << "text object not found.\n"; + } + + return q_app.exec (); +} + diff --git a/qml/hw.qml b/qml/hw.qml index 695810e..91868de 100644 --- a/qml/hw.qml +++ b/qml/hw.qml @@ -7,6 +7,7 @@ Rectangle { Text { id: helloText + objectName: "helloText" text: "Hello world!" y: 30 anchors.horizontalCenter: page.horizontalCenter