Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Model based on QList containing objects of custom class

    General and Desktop
    4
    10
    2239
    Loading More Posts
    • 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
      diredko last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @SGaist last edited by

            he did
            Q_DECLARE_METATYPE(Move)

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 1
              • D
                diredko last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • D
                    diredko last edited by diredko

                    @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 Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 0
                      • U
                        ultramanjones @diredko last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post