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. QList<QObject*> as member that can be used as a list
Forum Updated to NodeBB v4.3 + New Features

QList<QObject*> as member that can be used as a list

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 2.3k Views 2 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    Okay, I have been playing around with using QList<QObject*> as a member of an object that acts like a list model in QML. This works and I can get to the custom properties defined in my SubObject that derives from QObject.

    Q_PROPERTY(QList<QObject*> subList READ subList NOTIFY subListChanged)
    

    However, when I try to use an object that inherits QObject I cannot use that object as a list:

    Q_PROPERTY(QList<SubObject*> subList READ subList NOTIFY subListChanged)
    

    I tried registering this SubObject class using:

    Q_DECLARE_METATYPE
    qmlRegisterType<SubObject>(); 
    qRegisterMetaType<QList<SubObject*>>();
    

    but I cannot get QML to recoqnize a new list type to use in a ListView as a model. The methods and properties seem to still work that define on the custom SubObject.

    Is QList<QObject*> just special?

    C++ is a perfectly valid school of magic.

    ODБOïO 1 Reply Last reply
    0
    • fcarneyF fcarney

      Okay, I have been playing around with using QList<QObject*> as a member of an object that acts like a list model in QML. This works and I can get to the custom properties defined in my SubObject that derives from QObject.

      Q_PROPERTY(QList<QObject*> subList READ subList NOTIFY subListChanged)
      

      However, when I try to use an object that inherits QObject I cannot use that object as a list:

      Q_PROPERTY(QList<SubObject*> subList READ subList NOTIFY subListChanged)
      

      I tried registering this SubObject class using:

      Q_DECLARE_METATYPE
      qmlRegisterType<SubObject>(); 
      qRegisterMetaType<QList<SubObject*>>();
      

      but I cannot get QML to recoqnize a new list type to use in a ListView as a model. The methods and properties seem to still work that define on the custom SubObject.

      Is QList<QObject*> just special?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      hi,
      @fcarney said in QList<QObject*> as member that can be used as a list:

      However, when I try to use an object that inherits QObject I cannot use that object as a list:

      you can pass your SubObjects to QML as QVariantList and use it as list model

      Q_PROPERTY(QVariantList selectedPrograms READ selectedPrograms NOTIFY selectedProgramsChanged)
      
         QVariantList selectedPrograms(){
             QVariantList progList;
             for(auto prg : m_selectedPrograms){
                 progList.append(QVariant::fromValue(prg));
             }
             return progList;
         }
      
      private :
       QList<Job*> m_selectedPrograms;
      
      
      GrecKoG 1 Reply Last reply
      3
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @LeLev said in QList<QObject*> as member that can be used as a list:

        you can pass your SubObjects to QML as QVariantList and use it as list model

        Holy crap that was a lot easier than what I was trying. I looked all over for this and never found something this simple. I have been looking for a light weight list type for QML that was not based on the abstract list models. This is perfect!

        Thanks.

        C++ is a perfectly valid school of magic.

        ODБOïO 1 Reply Last reply
        0
        • fcarneyF fcarney

          @LeLev said in QList<QObject*> as member that can be used as a list:

          you can pass your SubObjects to QML as QVariantList and use it as list model

          Holy crap that was a lot easier than what I was trying. I looked all over for this and never found something this simple. I have been looking for a light weight list type for QML that was not based on the abstract list models. This is perfect!

          Thanks.

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @fcarney I'm glad it helped

          1 Reply Last reply
          0
          • ODБOïO ODБOï

            hi,
            @fcarney said in QList<QObject*> as member that can be used as a list:

            However, when I try to use an object that inherits QObject I cannot use that object as a list:

            you can pass your SubObjects to QML as QVariantList and use it as list model

            Q_PROPERTY(QVariantList selectedPrograms READ selectedPrograms NOTIFY selectedProgramsChanged)
            
               QVariantList selectedPrograms(){
                   QVariantList progList;
                   for(auto prg : m_selectedPrograms){
                       progList.append(QVariant::fromValue(prg));
                   }
                   return progList;
               }
            
            private :
             QList<Job*> m_selectedPrograms;
            
            
            GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #5

            @LeLev said in QList<QObject*> as member that can be used as a list:

            you can pass your SubObject to QML as QVariantList and use it as list model

            How is that better than a QObjectList?

            ODБOïO 1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              @GrecKo said in QList<QObject*> as member that can be used as a list:

              How is that better than a QObjectList?

              I think QObjectList is just a QList<QObject*> alias from what I read. I think internally the QVariant is probably just seeing my SubObject* as QObject*. But you can add non-qobject based types with Q_DECLARE_METATYPE. So QVariant version would be able to handle that.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                Yes QObjectList is just a QList<QObject*>, but in your case, QVariantList is just adding another level of indirection.

                If you don't mind using a 3rd party library and a bit more intrusive solution, you can use : QQmlObjectListModel from http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models .

                It behaves as a QList<T*> on the c++ side but also expose a QAIM (QAbstractItemModel) interface emitting the correct signals so you can plug a QML view onto it.

                As for your original question, yes QList<QObject*> is specially handled by QML views.

                1 Reply Last reply
                2
                • GrecKoG GrecKo

                  @LeLev said in QList<QObject*> as member that can be used as a list:

                  you can pass your SubObject to QML as QVariantList and use it as list model

                  How is that better than a QObjectList?

                  ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by
                  #8

                  @GrecKo said in QList<QObject*> as member that can be used as a list:

                  How is that better than a QObjectList?

                  i don't know, tell me

                  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