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 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?

    JonBJ 1 Reply Last reply
    0
    • T Tobias83

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      1
      • T Tobias83

        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 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 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
          0
          • JonBJ JonB

            @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 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

              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 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
              1
              • B Bob64

                @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 last edited by
                #14

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

                1 Reply Last reply
                0
                • B Bob64

                  @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 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

                  JonBJ T 2 Replies Last reply
                  0
                  • T Tobias83

                    @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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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

                      @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 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?

                      JonBJ 1 Reply Last reply
                      0
                      • T Tobias83

                        @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?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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 last edited by
                          #19
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            Tobias83
                            wrote on 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

                            • Login

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