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