Duplication of object in C++ and QML

  • Hello, I'm working on an application that has both C++ and QML components. The application has a 'parsing' class that interprets data from another process. This data is sent from the parser to a different, but relevant class for processing, then displayed in the QML frontend. Right now I handle the passing of the data from the parser by having the data as a class member variable, which is further represented as a QML property. From there it is passed to a QML instance of the data processing class. Essentially, I perform my connect() call in QML when it could be done in C++.

    This means that my parsing class has member variables of everything that comes in from the other process, as well as that data being a member variable of the relevant class. This essentially duplicates all of my data.

    I would like to be able to connect the 'value received' signal from the parser to a slot in the relevant processing class, then have the processing class handle the QML properties of itself. However, I've been having trouble representing an object created in C++ represented in QML. I can create an object of that class in QML, and process that data in C++, but not the other way around.

    Is there a way to have the same object be represented simultaneously in C++ and QML?

  • AHA! In QML, wrap the QObject-derived classes that you made in an Item{} and findChild will find them just fine.

  • Lifetime Qt Champion

    Do you mean like the sample ?

    class ApplicationData : public QObject
        Q_INVOKABLE QDateTime getCurrentDateTime() const {
            return QDateTime::currentDateTime();
        QGuiApplication app(argc, argv);
        QQuickView view;
        ApplicationData data;
        return app.exec();

  • I've considered that, but I'd much rather have the property of the QML object be set by a signal from the parser, rather than by calling the parser directly.

  • Lifetime Qt Champion

    Ok, sounds like you already tried the doc stuff ?
    "Accessing Loaded QML Objects by Object Name"
    Like find by name and since its a qobject, cant you just bind the c++ parsers
    signal to a property in the object ?
    I assume its some more complicated you want and Im afraid its outside my territory :)

    Lets see what others suggest

  • I have mostly tried the doc stuff, I'll keep with it and post any solutions I find

  • So, I've found that I cannot view my QML object in C++ if the QML object's parent is not the root object. When I created the class, I left the default QObject inheritance as a nullptr, and that extended to when I created the object in QML.


Log in to reply