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. Model based on QList containing objects of custom class
Forum Updated to NodeBB v4.3 + New Features

Model based on QList containing objects of custom class

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.8k Views 3 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.
  • D Offline
    D Offline
    diredko
    wrote on last edited by
    #1

    Hi All,
    I need to expose a QList containing objects of custom class to QML as a model. The problem is that when I'm trying to do this qml 'says' that reference to model cannot be resolved. Would someone please help me out?

    The custom class is defined as follows:

    #include <QMetaType>
    
    #include <QObject>
    
    class Move
    {
    
        Q_GADGET
        Q_PROPERTY(int fromIndex READ getFrom)
        Q_PROPERTY(int toIndex READ getTo)
    
    private:
        int from;
        int to;
    
    protected:
    public:
        Move(int _from, int _to);
        Move():from(0), to(0){}
        Move(const Move& move):from(move.getFrom()), to(move.getTo()){}
        ~Move() {}
    
        int getFrom() const;
        int getTo() const;
    };
    
    Q_DECLARE_METATYPE(Move)
    



    This is how I define a list which I'm going to expose as a model.

    QList<Move>  moves;
    

    I'm exposing the model to QML as below. With this code I receive a complaint from QML -
    qrc:/qmls/Main.qml:39: ReferenceError: Moves is not defined

    QQmlContext* cntx = engine.rootContext();
    
    if(cntx != nullptr)
    {
        cntx->setContextProperty("Moves", QVariant::fromValue(moves));
    }
    



    When I'm passing a number (say 64) as a model I can see that model is recognized and QML is inflated with items:

    QQmlContext* cntx = engine.rootContext();
    
    if(cntx != nullptr)
    {
        cntx->setContextProperty("Moves", QVariant::fromValue(64));
    }
    

    Thanks.

    U 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      It all seems fine.
      You should delete ur build folder and make sure all is recompiled.
      Make sure there is a moc_ file for where Move lives.

      Also test that QVariant do like your type
      QVariant v = QVariant::fromValue(moves);

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Did you register Moves ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        mrjjM 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Did you register Moves ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          he did
          Q_DECLARE_METATYPE(Move)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I was thinking about qmlRegisterType like shown in Defining QML Types from C++.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • D Offline
              D Offline
              diredko
              wrote on last edited by
              #6

              Hi,
              Thanks for your suggestion. I have added the following line:

              qmlRegisterType<Move>();
              

              but it didn't help. I'm still getting same error - qrc:/qmls/Main.qml:39: ReferenceError: Moves is not defined

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                How are you using that type in your QML code ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  diredko
                  wrote on last edited by diredko
                  #8

                  @SGaist
                  I'm not using Move type in QML directly. I'm only accessing model which is a QList<Move> as below:

                  Column
                  {
                        Repeater
                        {
                            model: Moves
                  
                            Text {height: 20; width: 20; text: "HELLO"}
                        }
                  }
                  

                  Also I tried a little "experiment" as @mrjj suggested:

                  Move move(1, 2);
                  
                  QVariant var = QVariant::fromValue(move);
                  
                  Move move1 = var.value<Move>();
                  
                  std::cout << move1.getFrom() << " " << move1.getTo() << std::endl;
                  

                  This code outputs "1 2" as expected

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Ok, then there are some small things to do in order to use that class directly from QML. See here.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • D diredko

                      Hi All,
                      I need to expose a QList containing objects of custom class to QML as a model. The problem is that when I'm trying to do this qml 'says' that reference to model cannot be resolved. Would someone please help me out?

                      The custom class is defined as follows:

                      #include <QMetaType>
                      
                      #include <QObject>
                      
                      class Move
                      {
                      
                          Q_GADGET
                          Q_PROPERTY(int fromIndex READ getFrom)
                          Q_PROPERTY(int toIndex READ getTo)
                      
                      private:
                          int from;
                          int to;
                      
                      protected:
                      public:
                          Move(int _from, int _to);
                          Move():from(0), to(0){}
                          Move(const Move& move):from(move.getFrom()), to(move.getTo()){}
                          ~Move() {}
                      
                          int getFrom() const;
                          int getTo() const;
                      };
                      
                      Q_DECLARE_METATYPE(Move)
                      



                      This is how I define a list which I'm going to expose as a model.

                      QList<Move>  moves;
                      

                      I'm exposing the model to QML as below. With this code I receive a complaint from QML -
                      qrc:/qmls/Main.qml:39: ReferenceError: Moves is not defined

                      QQmlContext* cntx = engine.rootContext();
                      
                      if(cntx != nullptr)
                      {
                          cntx->setContextProperty("Moves", QVariant::fromValue(moves));
                      }
                      



                      When I'm passing a number (say 64) as a model I can see that model is recognized and QML is inflated with items:

                      QQmlContext* cntx = engine.rootContext();
                      
                      if(cntx != nullptr)
                      {
                          cntx->setContextProperty("Moves", QVariant::fromValue(64));
                      }
                      

                      Thanks.

                      U Offline
                      U Offline
                      ultramanjones
                      wrote on last edited by
                      #10

                      @diredko What is the answer? This thread is incomplete, 4 years old or not.

                      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