Creating a C++ identifier for a dynamically generated object
-
You could do something like this:
class BackEnd : public QObject { Q_OBJECT Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) public:
//main.cpp #include <QQmlContext> //... QQmlApplicationEngine engine; BackEnd myBackend; engine.rootContext()->setContextProperty("BACKEND", &myBackend);
//main.qml - quick and dirty example import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 import QtQuick.Layouts 1.12 Window { id: app visible: true width: 640 height: 480 ColumnLayout { RowLayout { Column { spacing: 10 Text { font.bold: true text: "Value from C++ backend:" } Text { // automatically re-evaluates based on the Q_PROPERTY NOTIFY signal text: BACKEND.userName } } Rectangle { border.width: 2 border.color: "green" width: childrenRect.width height: childrenRect.height TextField { id: unserNameInput padding: 10 color: "black" placeholderText: "Enter Username" } } } Button { Layout.alignment: Qt.AlignHCenter padding: 20 text: "SAVE" onClicked: { // uses the Q_PROPERTY WRITE method BACKEND.userName = unserNameInput.text } } } // in case you need any additional signal handling Connections { target: BACKEND function onUserNameChanged() { console.debug("BackEnd signal:", "userNameChanged") } } }
-
@lemons said in Creating a C++ identifier for a dynamically generated object:
engine.rootContext()->setContextProperty("BACKEND", &myBackend);
It is a little off putting that your "backend" is yelling at you. ;-)
Good example, btw. -
You could do something like this:
class BackEnd : public QObject { Q_OBJECT Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) public:
//main.cpp #include <QQmlContext> //... QQmlApplicationEngine engine; BackEnd myBackend; engine.rootContext()->setContextProperty("BACKEND", &myBackend);
//main.qml - quick and dirty example import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 import QtQuick.Layouts 1.12 Window { id: app visible: true width: 640 height: 480 ColumnLayout { RowLayout { Column { spacing: 10 Text { font.bold: true text: "Value from C++ backend:" } Text { // automatically re-evaluates based on the Q_PROPERTY NOTIFY signal text: BACKEND.userName } } Rectangle { border.width: 2 border.color: "green" width: childrenRect.width height: childrenRect.height TextField { id: unserNameInput padding: 10 color: "black" placeholderText: "Enter Username" } } } Button { Layout.alignment: Qt.AlignHCenter padding: 20 text: "SAVE" onClicked: { // uses the Q_PROPERTY WRITE method BACKEND.userName = unserNameInput.text } } } // in case you need any additional signal handling Connections { target: BACKEND function onUserNameChanged() { console.debug("BackEnd signal:", "userNameChanged") } } }
-
@Snorlax you have to set the context property before loading the initial url in main.cpp
int main(int argc, char *argv[]) { // ... QApplication app(argc, argv); // ... QQmlApplicationEngine engine; // ... BackEnd myBackend; engine.rootContext()->setContextProperty("BACKEND", &myBackend); // ... // context property used in connection must be set before this line engine.load(url); // ... return app.exec(); }
-
@Snorlax you have to set the context property before loading the initial url in main.cpp
int main(int argc, char *argv[]) { // ... QApplication app(argc, argv); // ... QQmlApplicationEngine engine; // ... BackEnd myBackend; engine.rootContext()->setContextProperty("BACKEND", &myBackend); // ... // context property used in connection must be set before this line engine.load(url); // ... return app.exec(); }
-
@Snorlax the signal is only triggered when the signal from the c++ file is emitted.
Therefore, you have to enter something in the input field and click the save button.As the example returns if you set the same name again, it won't emit a signal if you click the save button multiple times without changing the input field value:
void BackEnd::setUserName(const QString &userName) { // do not proceed if current name is equal to new name if (userName == m_userName) return; m_userName = userName; emit userNameChanged(); }