Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Possible to wrap non-QObject C++ object by an Entrymodel?
Forum Updated to NodeBB v4.3 + New Features

Possible to wrap non-QObject C++ object by an Entrymodel?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
20 Posts 5 Posters 1.3k 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
    Tobias83
    wrote on 9 Jul 2024, 19:14 last edited by
    #1

    Hallo,

    I still want to wrap a C++ object by an Entrymodel. The C++ object is a Particle System which I want to order time steps. I want that every state of the C++ object is preserved by the Entrymodel constructur. If I init the Particle System in the Entrymodel constructor it freezes at the initial state. So I started with Qt5 and an overloaded constructor for the Entrymodel - the parameters were (QObject, System*).

    This worked for me in Qt5, but not in Qt6. I already did a Post. But there was no good solution for me presented:
    Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable

    So, is there a way at all to wrap a non-QObject C++ Object in an Qt Entrymodel?

    Kind regards,
    Tobias

    B 1 Reply Last reply 12 Jul 2024, 15:47
    0
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 10 Jul 2024, 12:36 last edited by
      #2

      We don't know your code, so we can't say whether it works or not.
      The QML element has to be default constructable. In your case, there is no default c'tor. Just one with two arguments. That's the issue.

      Software Engineer
      The Qt Company, Oslo

      T 1 Reply Last reply 10 Jul 2024, 13:29
      0
      • A Axel Spoerl
        10 Jul 2024, 12:36

        We don't know your code, so we can't say whether it works or not.
        The QML element has to be default constructable. In your case, there is no default c'tor. Just one with two arguments. That's the issue.

        T Offline
        T Offline
        Tobias83
        wrote on 10 Jul 2024, 13:29 last edited by
        #3

        @Axel-Spoerl Dear Axel Spoerl,
        ... and I cannot make the constructor with two arguments to the default constructor? In Qt5 this worked for me. I want to wrap my System* pointer, because the current state of the system is transferred by the constructor.
        I presented some (complicate) code in stackoverflow:
        Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable

        A 1 Reply Last reply 11 Jul 2024, 07:02
        0
        • T Offline
          T Offline
          Tobias83
          wrote on 10 Jul 2024, 13:43 last edited by
          #4

          I dont know the working example in Qt5 not well anymore, because it was another simulation which I modified since that.
          But must the System* pointer to be a virtual class member of the entrymodel? If I compile Qt programs there is some virtualizaion, isn't it?

          1 Reply Last reply
          0
          • T Tobias83
            10 Jul 2024, 13:29

            @Axel-Spoerl Dear Axel Spoerl,
            ... and I cannot make the constructor with two arguments to the default constructor? In Qt5 this worked for me. I want to wrap my System* pointer, because the current state of the system is transferred by the constructor.
            I presented some (complicate) code in stackoverflow:
            Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable

            A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote on 11 Jul 2024, 07:02 last edited by Axel Spoerl 7 Nov 2024, 07:04
            #5

            @Tobias83 said in Possible to wrap non-QObject C++ object by an Entrymodel?:

            ... and I cannot make the constructor with two arguments to the default constructor?

            That's a contradiction in itself. A default constructor must be callable without arguments.

            The QML engine creates qml elements by calling its default constructor without arguments. That's not possible in your case, which is what the error says. I actually doubt that it worked in Qt5, but feel free to prove me wrong.

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            0
            • G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 11 Jul 2024, 09:03 last edited by
              #6

              I'm still not sure what your actual need is after reading this post and your previous one.

              Are you sure you want the object to be instantiated by QML, can't you instantiate it in C++ and expose to QML as singleton?
              If you need it to be instantiated by QML with some parameters, can't you use properties for that instead?

              I'm also having a hard time seeing how this could have worked in Qt 5, not much has changed on this subject.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tobias83
                wrote on 12 Jul 2024, 12:15 last edited by
                #7

                I want to write a particle simulation of hard spheres. Therefor I want to get out two executebles: one fast without Qt and without graphical output, and one with Qt and with graphical output. For the second I want to wrap the System class of the first as a pointer in the Entrymodel and draw the spheres by QML.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tobias83
                  wrote on 12 Jul 2024, 13:07 last edited by
                  #8

                  A question for understanding Qt: As I understood, there is a continuous process generating the Entrymodel object by its QObject parent, right? So if I have a constructor Entrymodel(QObject parent*), the previous state of the Entrymodel (parent) is parameter for the next, right? And this happens in a continuous loop?

                  J 1 Reply Last reply 12 Jul 2024, 13:12
                  0
                  • T Tobias83
                    12 Jul 2024, 13:07

                    A question for understanding Qt: As I understood, there is a continuous process generating the Entrymodel object by its QObject parent, right? So if I have a constructor Entrymodel(QObject parent*), the previous state of the Entrymodel (parent) is parameter for the next, right? And this happens in a continuous loop?

                    J Offline
                    J Offline
                    JonB
                    wrote on 12 Jul 2024, 13:12 last edited by
                    #9

                    @Tobias83
                    I know nothing about QML or Entrymodel, so excuse me if there is something special about that. But speaking for Qt at least there is no "continuous process generating" anything, and the parent argument to a QObject constructor simply sets up the object model hierarchy for parent/child, there is no "copying of state" between parent & child (unless you code something yourself).

                    T 1 Reply Last reply 12 Jul 2024, 16:37
                    1
                    • T Tobias83
                      9 Jul 2024, 19:14

                      Hallo,

                      I still want to wrap a C++ object by an Entrymodel. The C++ object is a Particle System which I want to order time steps. I want that every state of the C++ object is preserved by the Entrymodel constructur. If I init the Particle System in the Entrymodel constructor it freezes at the initial state. So I started with Qt5 and an overloaded constructor for the Entrymodel - the parameters were (QObject, System*).

                      This worked for me in Qt5, but not in Qt6. I already did a Post. But there was no good solution for me presented:
                      Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable

                      So, is there a way at all to wrap a non-QObject C++ Object in an Qt Entrymodel?

                      Kind regards,
                      Tobias

                      B Offline
                      B Offline
                      Bob64
                      wrote on 12 Jul 2024, 15:47 last edited by
                      #10

                      @Tobias83 Is Entrymodel something specific to your project? I can't see anything related to Qt with this name. If it is something specific, nobody here is going to know what this is. It is difficult to understand what you are asking about. Is there any way that you can try to extract the essence of the issue that you are having without discussing specific details of your application? To me, it feels like there is probably a solution to what you want to do but we need a better way to understand it.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Tobias83
                        wrote on 12 Jul 2024, 16:25 last edited by
                        #11

                        Oh, I'm sorry! I thought Entrymodel is a common Qt name. I do this:
                        class Hardcore_Entrymodel : public QAbstractListModel

                        B 1 Reply Last reply 12 Jul 2024, 17:51
                        0
                        • J JonB
                          12 Jul 2024, 13:12

                          @Tobias83
                          I know nothing about QML or Entrymodel, so excuse me if there is something special about that. But speaking for Qt at least there is no "continuous process generating" anything, and the parent argument to a QObject constructor simply sets up the object model hierarchy for parent/child, there is no "copying of state" between parent & child (unless you code something yourself).

                          T Offline
                          T Offline
                          Tobias83
                          wrote on 12 Jul 2024, 16:37 last edited by
                          #12

                          @JonB Hello JonB,
                          OK, it's not? I thought its a bit like OpenGL - there is such a loop I think ...

                          1 Reply Last reply
                          0
                          • T Tobias83
                            12 Jul 2024, 16:25

                            Oh, I'm sorry! I thought Entrymodel is a common Qt name. I do this:
                            class Hardcore_Entrymodel : public QAbstractListModel

                            B Offline
                            B Offline
                            Bob64
                            wrote on 12 Jul 2024, 17:51 last edited by
                            #13

                            @Tobias83 OK, so Entrymodel is just part of the name of your class which implements a QAbstractListModel.

                            How is your list model used in your application? If you want to expose it to QML so that you can actually instantiate it in QML, like this

                            ListModel {
                                model: Hardcore_EntryModel {
                                }
                                ...
                            }
                            

                            then you are restricted to not being able to pass arguments into your constructor.

                            On the other hand if you want to be able to instantiate your model in C++ and just make that instance accessible in QML, you are potentially less restricted. In your C++ you can instantiate your Hardcore_EntryModel however you want and use qmlRegisterSingletonInstance:

                            Hardcore_EntryModel entryModel(arg1, arg2, ...);
                            qmlRegisterSingletonInstance("mytypes", 1, 0, "EntryModel", &entryModel);
                            

                            and then use it in your QML:

                            ListModel {
                                model: EntryModel // use the singleton instance exposed in the C++
                                ...
                            }
                            
                            T 2 Replies Last reply 14 Jul 2024, 16:43
                            1
                            • B Bob64
                              12 Jul 2024, 17:51

                              @Tobias83 OK, so Entrymodel is just part of the name of your class which implements a QAbstractListModel.

                              How is your list model used in your application? If you want to expose it to QML so that you can actually instantiate it in QML, like this

                              ListModel {
                                  model: Hardcore_EntryModel {
                                  }
                                  ...
                              }
                              

                              then you are restricted to not being able to pass arguments into your constructor.

                              On the other hand if you want to be able to instantiate your model in C++ and just make that instance accessible in QML, you are potentially less restricted. In your C++ you can instantiate your Hardcore_EntryModel however you want and use qmlRegisterSingletonInstance:

                              Hardcore_EntryModel entryModel(arg1, arg2, ...);
                              qmlRegisterSingletonInstance("mytypes", 1, 0, "EntryModel", &entryModel);
                              

                              and then use it in your QML:

                              ListModel {
                                  model: EntryModel // use the singleton instance exposed in the C++
                                  ...
                              }
                              
                              T Offline
                              T Offline
                              Tobias83
                              wrote on 14 Jul 2024, 16:43 last edited by
                              #14

                              @Bob64 Thanks! I will try this tomorrow ...

                              1 Reply Last reply
                              0
                              • B Bob64
                                12 Jul 2024, 17:51

                                @Tobias83 OK, so Entrymodel is just part of the name of your class which implements a QAbstractListModel.

                                How is your list model used in your application? If you want to expose it to QML so that you can actually instantiate it in QML, like this

                                ListModel {
                                    model: Hardcore_EntryModel {
                                    }
                                    ...
                                }
                                

                                then you are restricted to not being able to pass arguments into your constructor.

                                On the other hand if you want to be able to instantiate your model in C++ and just make that instance accessible in QML, you are potentially less restricted. In your C++ you can instantiate your Hardcore_EntryModel however you want and use qmlRegisterSingletonInstance:

                                Hardcore_EntryModel entryModel(arg1, arg2, ...);
                                qmlRegisterSingletonInstance("mytypes", 1, 0, "EntryModel", &entryModel);
                                

                                and then use it in your QML:

                                ListModel {
                                    model: EntryModel // use the singleton instance exposed in the C++
                                    ...
                                }
                                
                                T Offline
                                T Offline
                                Tobias83
                                wrote on 15 Jul 2024, 12:48 last edited by Tobias83
                                #15

                                @Bob64 Dear Bob64,
                                How do I have to handle this contructor:

                                explicit Hardcore_Entrymodel(QObject *parent=0, System* system);
                                

                                Is the =0 correct?
                                If I call it like this, it errors:

                                tw_hardcoresystem::Hardcore_EntryModel entryModel(nullptr, &system);
                                

                                It also errors with this constructor, because there is a QAbstractListModel(parent) which needs the parent argument:

                                explicit Hardcore_Entrymodel(System* system);
                                

                                Thank you for your Help!

                                Tobias

                                J T 2 Replies Last reply 15 Jul 2024, 12:59
                                0
                                • T Tobias83
                                  15 Jul 2024, 12:48

                                  @Bob64 Dear Bob64,
                                  How do I have to handle this contructor:

                                  explicit Hardcore_Entrymodel(QObject *parent=0, System* system);
                                  

                                  Is the =0 correct?
                                  If I call it like this, it errors:

                                  tw_hardcoresystem::Hardcore_EntryModel entryModel(nullptr, &system);
                                  

                                  It also errors with this constructor, because there is a QAbstractListModel(parent) which needs the parent argument:

                                  explicit Hardcore_Entrymodel(System* system);
                                  

                                  Thank you for your Help!

                                  Tobias

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 15 Jul 2024, 12:59 last edited by
                                  #16

                                  @Tobias83 said in Possible to wrap non-QObject C++ object by an Entrymodel?:

                                  explicit Hardcore_Entrymodel(QObject *parent=0, System* system);

                                  Per C++, any optional/default-value parameters can only come after any mandatory ones. Your code is not legal because a parameter without a default comes after one with a default. Either give a default for System* system as well, or (preferable IMO) make the system parameter come before the parent one. I would always leave a QObject-derived constructor taking QObject *parent = nullptr as the last argument.

                                  1 Reply Last reply
                                  1
                                  • T Tobias83
                                    15 Jul 2024, 12:48

                                    @Bob64 Dear Bob64,
                                    How do I have to handle this contructor:

                                    explicit Hardcore_Entrymodel(QObject *parent=0, System* system);
                                    

                                    Is the =0 correct?
                                    If I call it like this, it errors:

                                    tw_hardcoresystem::Hardcore_EntryModel entryModel(nullptr, &system);
                                    

                                    It also errors with this constructor, because there is a QAbstractListModel(parent) which needs the parent argument:

                                    explicit Hardcore_Entrymodel(System* system);
                                    

                                    Thank you for your Help!

                                    Tobias

                                    T Offline
                                    T Offline
                                    Tobias83
                                    wrote on 15 Jul 2024, 13:50 last edited by
                                    #17

                                    @Tobias83 I now found an a little bit different solution. In stackoverflow someone suggested set and get functions. So I have:

                                    public:
                                        explicit Hardcore_Entrymodel();
                                        //explicit Hardcore_Entrymodel(QObject *parent, System* system);
                                        void setSystem(System* system);
                                    

                                    and in the main fct:

                                        Hardcore_Entrymodel entryModel;
                                        entryModel.setSystem(&system);
                                        qmlRegisterSingletonInstance("hardcoresystem", 1, 0, "Hardcoreentrymodel", &entryModel);
                                    
                                    

                                    For this, it draws a window and the "QML Object is not creatable" error disappears.

                                    But there are still some little mistake, which are not related to Qt, I think.

                                    Thanks a lot until here!!!

                                    kind regards,
                                    Tobias

                                    PS: if the std::stof function in one case outputs the double value of a string, and some loops later gives the integer value - it is not related to Qt? What can this be? A corrupt compiler?

                                    J 1 Reply Last reply 15 Jul 2024, 13:55
                                    0
                                    • T Tobias83
                                      15 Jul 2024, 13:50

                                      @Tobias83 I now found an a little bit different solution. In stackoverflow someone suggested set and get functions. So I have:

                                      public:
                                          explicit Hardcore_Entrymodel();
                                          //explicit Hardcore_Entrymodel(QObject *parent, System* system);
                                          void setSystem(System* system);
                                      

                                      and in the main fct:

                                          Hardcore_Entrymodel entryModel;
                                          entryModel.setSystem(&system);
                                          qmlRegisterSingletonInstance("hardcoresystem", 1, 0, "Hardcoreentrymodel", &entryModel);
                                      
                                      

                                      For this, it draws a window and the "QML Object is not creatable" error disappears.

                                      But there are still some little mistake, which are not related to Qt, I think.

                                      Thanks a lot until here!!!

                                      kind regards,
                                      Tobias

                                      PS: if the std::stof function in one case outputs the double value of a string, and some loops later gives the integer value - it is not related to Qt? What can this be? A corrupt compiler?

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 15 Jul 2024, 13:55 last edited by
                                      #18

                                      @Tobias83
                                      std::stof() has nothing to do with Qt, you do not have a "corrupt compiler" and even if you did that would not change its behaviour according to how many times it is called. It returns a float. It does not return an int, though of course a value returned might happen to be an integer (as in, no decimal places).

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        Tobias83
                                        wrote on 15 Jul 2024, 14:10 last edited by
                                        #19
                                        This post is deleted!
                                        1 Reply Last reply
                                        0
                                        • T Offline
                                          T Offline
                                          Tobias83
                                          wrote on 19 Jul 2024, 19:32 last edited by
                                          #20

                                          Does the QAbstractListModel work the same as Singleton if data is changed. In QML I have a Repeater which draws the particles. But if I do a simulation step, the picture in in the window is not updated, but the coordinates in cpp are. On the console I get the output qml: DataChanged received.

                                          This my step method:

                                          void Hardcore_Entrymodel::doNewStep()
                                          {
                                              emit newStepDoing();
                                              hardcoresystem->testStep();
                                              populate();
                                          
                                              return;
                                          }
                                          

                                          and populate looks like that:

                                          void Hardcore_Entrymodel::populate()
                                          {
                                              int ipart;
                                              for(ipart=0; ipart<nparticles; ipart++){
                                                  mDatas[ipart]= initqtview.SystemToWindow(hardcoresystem->returnIParticle(ipart));
                                              }
                                              emit dataChanged(index(0),index(nparticles));
                                              return;
                                          }
                                          

                                          And the step is made by mouseclick in qml:

                                          MouseArea {
                                                      anchors.fill: parent
                                                      onClicked: Hardcoreentrymodel.doNewStep()
                                                  }
                                          
                                          1 Reply Last reply
                                          0

                                          1/20

                                          9 Jul 2024, 19:14

                                          • Login

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