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. Can I use a pointer in the ListView?

Can I use a pointer in the ListView?

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 3.5k 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.
  • H Offline
    H Offline
    harlentan
    wrote on last edited by
    #1

    As is description in my last post "http://developer.qt.nokia.com/forums/viewthread/8638/":http://developer.qt.nokia.com/forums/viewthread/8638/

    A new Page Will be created dynamically, and every Page will have its own C++ model to provide data for it.
    But we must use this kind of way to expose my model object to the QML context:

    @QDeclarativeContext *ctxt = view.rootContext();

    ctxt->setContextProperty("myModel", &model);@

    Because the C++ model will also created dynamically. And every name string of a model expose to QML, as is string "mymodle"here, must be unique. So how Can create my C++ model dynamically and expose to QML to use? Can I expose the pointer of the model not the string name "mymodel" to QML?

    For example, I have a button. If I push the button 10 times, it will create 10 model for me and expose to the QML to use. Certainly, we cannot use "mymodel" in all the model created dynamically. If I transfer the pointer of the C++ model to the ListView, not by the string name "mymodel", it will be a very good thing.

    Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      Hi,

      Would something like the following work in your case?

      @
      Page {
      //assign the model to this when it is created, rather than creating a property on the root context.
      property variant myModel

      ListView {
          model: myModel
      }
      

      }
      @

      Regards,
      Michael

      1 Reply Last reply
      0
      • H Offline
        H Offline
        harlentan
        wrote on last edited by
        #3

        [quote author="mbrasser" date="1313020617"]Hi,

        Would something like the following work in your case?

        @
        Page {
        //assign the model to this when it is created, rather than creating a property on the root context.
        property variant myModel

        ListView {
            model: myModel
        }
        

        }
        @

        Regards,
        Michael[/quote]

        you mean I can assign the pointer of my C++ model to the property variant myModel?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          Yes, for example something like:

          @
          Page {
          property variant myModel

          ListView {
              model: myModel
          }
          
          Button { onClicked: myModel = myApp.createModel() }
          

          }
          @

          where createModel returns a pointer to your model object.

          Regards,
          Michael

          1 Reply Last reply
          0
          • H Offline
            H Offline
            harlentan
            wrote on last edited by
            #5

            [quote author="mbrasser" date="1313030598"]Yes, for example something like:

            @
            Page {
            property variant myModel

            ListView {
                model: myModel
            }
            
            Button { onClicked: myModel = myApp.createModel() }
            

            }
            @

            where createModel returns a pointer to your model object.

            Regards,
            Michael[/quote]

            I use it as below:

            @
            Rectangle{
            property variant mymodel
            ListView {
            //...other codes

                     delegate: mydelegate
                     model: mymodel
                     cacheBuffer: 40
                     }
            
                     Component.onCompleted:
                    {
            
                    mymodel = myApp.createModel()
                    console.log("load model complete")
                    }
            

            }
            @

            But there is no data on the UI. I don't know why.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              Have you verified that mymodel is being set correctly (by e.g. console.logging it)? You might need to register your model type (or have the signature of createModel be QObject *createModel() ) to get things working correctly.

              Note that you could also assign directly to the ListView's model (mymodel isn't strictly necessary), e.g myListView.model = myApp.createModel().

              Regards,
              Michael

              1 Reply Last reply
              0
              • H Offline
                H Offline
                harlentan
                wrote on last edited by
                #7

                [quote author="mbrasser" date="1313041822"]Have you verified that mymodel is being set correctly (by e.g. console.logging it)? You might need to register your model type (or have the signature of createModel be QObject *createModel() ) to get things working correctly. Note that you could also assign directly to the ListView's model (mymodel isn't strictly necessary), e.g myListView.model = myApp.createModel(). Regards, Michael[/quote]

                The model that created is c++ model.
                It works well as below:
                @
                DataModel *p = new DataModel;
                rootContext->setContextProerty("myModel", p);
                //Then, reference myModel in the ListView, it works well
                @

                So. in some degree, I let myApp to create the model:

                @
                //DataMode is a subclass of the QAbstractListModel, it works well.
                DataModel * myApp::createModel()
                {
                return new DataModel;
                }
                @
                So, should I regist my model type?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mbrasser
                  wrote on last edited by
                  #8

                  Yes, I think both of those things are worth trying (registering the type or changing the signature as mentioned above) to see if that gets things working for you (though I can't say for certain that that is the problem).

                  Regards,
                  Michael

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    harlentan
                    wrote on last edited by
                    #9

                    Hi Michael,

                    Thanks for your reply. The thought you provide to me is very important for me.
                    And I get over it at last.
                    I just regist my Model Type, and all is well now.

                    Thanks. :)

                    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