Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Duplication of object in C++ and QML
Forum Updated to NodeBB v4.3 + New Features

Duplication of object in C++ and QML

Scheduled Pinned Locked Moved Solved General and Desktop
qmlsignals & slots
7 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    twall
    wrote on 12 Jan 2018, 17:41 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      twall
      wrote on 15 Jan 2018, 21:57 last edited by
      #7

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 12 Jan 2018, 17:56 last edited by
        #2

        Hi
        Do you mean like the sample ?
        http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

        class ApplicationData : public QObject
        {
            Q_OBJECT
        public:
            Q_INVOKABLE QDateTime getCurrentDateTime() const {
                return QDateTime::currentDateTime();
            }
        };
        
        int main(int argc, char *argv[]) {
            QGuiApplication app(argc, argv);
        
            QQuickView view;
        
            ApplicationData data;
            view.rootContext()->setContextProperty("applicationData", &data);
        
            view.setSource(QUrl::fromLocalFile("MyItem.qml"));
            view.show();
        
            return app.exec();
        }
        
        
        1 Reply Last reply
        1
        • T Offline
          T Offline
          twall
          wrote on 12 Jan 2018, 19:06 last edited by
          #3

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 12 Jan 2018, 19:41 last edited by
            #4

            Ok, sounds like you already tried the doc stuff ?
            https://doc-snapshots.qt.io/qt5-5.9/qtqml-cppintegration-interactqmlfromcpp.html
            "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

            1 Reply Last reply
            1
            • T Offline
              T Offline
              twall
              wrote on 15 Jan 2018, 13:45 last edited by
              #5

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

              1 Reply Last reply
              0
              • T Offline
                T Offline
                twall
                wrote on 15 Jan 2018, 20:13 last edited by
                #6

                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.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  twall
                  wrote on 15 Jan 2018, 21:57 last edited by
                  #7

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

                  1 Reply Last reply
                  1

                  7/7

                  15 Jan 2018, 21:57

                  • Login

                  • Login or register to search.
                  7 out of 7
                  • First post
                    7/7
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved