-1

I created an Hello World app in QML. Now I want to learn how to modify the text from "Hello World" to "Goodbye World" from C++.

The qml looks like so:

import QtQuick 2.6

Rectangle {
    property alias mouseArea: mouseArea

    width: 360
    height: 360

    MouseArea {
        id: mouseArea
        anchors.fill: parent
    }

    Text {
        id: helloText
        anchors.centerIn: parent
        text: "Hello World"
    }
}

I've attempted to follow the

https://wiki.qt.io/Introduction_to_Qt_Quick#Integration_with_C.2B.2B_applications

But no luck. The code seems incomplete. For example, it leaves off information with ellipses like so:

QDeclarativeContext *context = …;

And I can't find the header for the QDeclarativeContext even if it didn't. I suspect the documentation is old, but I'm not sure.

Anyway, I just want to see a simple example that lets me change the text from "Hello World" to "Goodbye World" from inside a C++ program.

dtech
  • 47,916
  • 17
  • 112
  • 190
Mitch
  • 1,716
  • 3
  • 25
  • 41

1 Answers1

2

That code is for the old QtQuick1 API, which was based on QGraphicsScene and is now outdated, obsolete and IIRC removed from Qt.

I would recommend against mingling with QML from C++, I'd even go further and call it an anti-pattern, in 99.9999% of the cases there is a better solution. You should keep the interaction between C++ and QML to a well defined API.

That being said, it is still possible to find objects and manipulate their properties. You can use QQmlApplicationEngine::rootObjects() to get access to the root objects, from there you can findChild() any object you have provided a objectName on the QML side, you can use QMetaObject:invokeMethod() (works for QML functions too!), use qobject_cast, set properties and whatnot.

All those techniques are covered in the documentation.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • I'm confused by your comment about "I would recommend against mingling with QML from C++". Do you mean you recommend against changing the UI display from C++? The term "mingling" is unclear to me. – Mitch Oct 11 '16 at 17:25
  • @Mitch - I mean "find object and do random stuff to it" - interaction between the two layers should be limited to signals, slots and properties. In a well designed API there wouldn't be any need to mingle with the GUI from C++ at all. – dtech Oct 11 '16 at 17:28
  • Your C++ code should be well encapsulated classes, which you simply "plug in" with QML using signals, slots and properties, you need good isolation and separation between the C++ logic and the QML GUI with a clearly defined interface for interconnection. Keep things where they belong, otherwise you create a bad design and invite trouble. – dtech Oct 11 '16 at 17:38
  • Okay, I think I understand what you mean by "mingle". I'm not sure how this applies in my case here, but thanks. I'll look at the link, but I have no choice as far as which side of the fence to code the changing of the values. Some values are calculated on the C++ side and I can't fix that. I need to get those values over to the display, which is in QML. I'm not building a API, I'm building an application. Perhaps making an API would be a good idea, but that's not the end goal. – Mitch Oct 11 '16 at 18:23
  • @Mitch - every half decent application that is above trivial outta have its own internal API built on top of the framework APIs, that's extensive and flexible design. In your particular case you shouldn't be searching for a QML object to change its text from C++, you should be binding the text to the property of a C++ object which changes the property internally. Which side does what? It is simple - the GUI side reads data from the C++ side, and direct user input to the C++ side for writing. That's what the GUI is for - to allow the user to see and interact with stuff. – dtech Oct 11 '16 at 18:31
  • I have built many applications over 20 years on pre-existing frameworks, if the framework had a sufficient API, there was no need to create my own API on top of it. In cases when the framework is insufficient, I needed to do that. I don't know QML or Qt so I'm not in a place to judge. In my case thousands of fields must change from C++ driven calculations. I sincerely hope the framework doesn't require me to create a class and instantiate a C++ object for each class as you imply. I'll learn how the mechanism works then and only then I'll make that judgement. Thanks for the link. I'll be back. – Mitch Oct 11 '16 at 18:50
  • Sounds like you need a good data model backbone, or... a good application design/API ;) I am working on a highly dynamic project which can have hundreds of thousands of data fields and I only have a 1000 lines of C++ or so. QML is just a different paradigm, it is very easy and fast to work with, but it is conceptually different from how stuff was being done the past 20 years. Many very experienced old timers are having basic problems with it, because the paradigm is new and different. Updating thousands of QML texts from C++ is **definitely the wrong approach**. – dtech Oct 11 '16 at 19:34