mp2.cpp: new example on getting properties

hw.qml: findChild uses objectName; so added it.
This commit is contained in:
Tal Lancaster
2017-01-18 15:53:29 -07:00
parent 7fdb2ff111
commit 558fba07a6
3 changed files with 50 additions and 1 deletions
+6 -1
View File
@@ -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)
+43
View File
@@ -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 <iostream>
#include <string>
#include <QGuiApplication>
#include <QQuickView>
#include <QtQuick>
#include <QtQml>
#include <QtCore>
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<QObject*> ("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 ();
}