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. declare property of pointer in qml
QtWS25 Last Chance

declare property of pointer in qml

Scheduled Pinned Locked Moved QML and Qt Quick
property
15 Posts 3 Posters 7.0k Views
  • 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
    themts
    wrote on last edited by themts
    #1

    Hey guys,

    I have a view with several edit fields. Now I want to instantiate such view and pass a datamodel to it which will be used to fill the edit-fields etc.
    How can I declare a property of pointer for my datamodel?

    If I try this:

    Item {
        id: thisIsMyCustomView
        width: 200
        height: 400
    
        property ClassNameOfModel dataModel
        //property ClassNameOfModel *dataModel  << This doesn't work
    
    ...
        TextField {
            text: dataModel.text1
            ...
        }
    
        TextField {
            text: dataModel.text2
            ...
        }
    

    it would instanciate an object of ClassNameOfModel.

    CU
    mts

    p3c0P 1 Reply Last reply
    0
    • T themts

      Hey guys,

      I have a view with several edit fields. Now I want to instantiate such view and pass a datamodel to it which will be used to fill the edit-fields etc.
      How can I declare a property of pointer for my datamodel?

      If I try this:

      Item {
          id: thisIsMyCustomView
          width: 200
          height: 400
      
          property ClassNameOfModel dataModel
          //property ClassNameOfModel *dataModel  << This doesn't work
      
      ...
          TextField {
              text: dataModel.text1
              ...
          }
      
          TextField {
              text: dataModel.text2
              ...
          }
      

      it would instanciate an object of ClassNameOfModel.

      CU
      mts

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @themts What is datamodel ?

      157

      1 Reply Last reply
      0
      • T Offline
        T Offline
        themts
        wrote on last edited by
        #3

        datamodel is a c-class.

        p3c0P 1 Reply Last reply
        0
        • T themts

          datamodel is a c-class.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @themts So you can set datamodel as a contextProperty. More info here.

          157

          1 Reply Last reply
          0
          • T Offline
            T Offline
            themts
            wrote on last edited by themts
            #5

            ok, I think it's not clear what I want.

            • I have a list with many objects in it (qList<ClassNameOfModel*>).
            • I display the list with a combobox (list is assigned as model to combobox)
            • I select one item in that combobox

            until here, no problem.

            Now I want to pass the selected item (ClassNameOfModel) to my common edit-component (qml-file) to display and edit some item-properties.
            This common edit-component looks like this:

            editComponent.qml:
            Item {
                property ClassNameOfModel *dataModel << This doesn't work
                TextField {
                    text: dataModel.text1
                    ...
                }
                TextField {
                    text: dataModel.text2
                    ...
                }
                TextField {
                    text: dataModel.text3
                    ...
                }
               ...
            }
            

            How can I pass data to my custom-component?

            p3c0P 1 Reply Last reply
            0
            • T themts

              ok, I think it's not clear what I want.

              • I have a list with many objects in it (qList<ClassNameOfModel*>).
              • I display the list with a combobox (list is assigned as model to combobox)
              • I select one item in that combobox

              until here, no problem.

              Now I want to pass the selected item (ClassNameOfModel) to my common edit-component (qml-file) to display and edit some item-properties.
              This common edit-component looks like this:

              editComponent.qml:
              Item {
                  property ClassNameOfModel *dataModel << This doesn't work
                  TextField {
                      text: dataModel.text1
                      ...
                  }
                  TextField {
                      text: dataModel.text2
                      ...
                  }
                  TextField {
                      text: dataModel.text3
                      ...
                  }
                 ...
              }
              

              How can I pass data to my custom-component?

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @themts Ok how do you call or load editComponent.qml ?

              157

              1 Reply Last reply
              0
              • T Offline
                T Offline
                themts
                wrote on last edited by
                #7

                ???

                editComponent {
                id: edit1
                }
                

                My idea was to use onCurrentIndexChanged to assign the selected model to my editComponent:

                    configComboBox.onCurrentIndexChanged: {
                        edit.dataModel: configComboBox.model[configComboBox.currentIndex]
                    }
                
                p3c0P 1 Reply Last reply
                0
                • T themts

                  ???

                  editComponent {
                  id: edit1
                  }
                  

                  My idea was to use onCurrentIndexChanged to assign the selected model to my editComponent:

                      configComboBox.onCurrentIndexChanged: {
                          edit.dataModel: configComboBox.model[configComboBox.currentIndex]
                      }
                  
                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @themts AFAIK you can do this

                  property ClassNameOfModel dataModel
                  

                  Have done something similar but with Rectangle.
                  What error do you get if you do it ?

                  157

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    themts
                    wrote on last edited by
                    #9

                    This declaration would create an instance of ClassNameOfModel.

                    p3c0P 1 Reply Last reply
                    0
                    • T themts

                      This declaration would create an instance of ClassNameOfModel.

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      @themts I guess no. You can check it by printing dataModel. It should be null.

                      157

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        themts
                        wrote on last edited by
                        #11

                        @themts:
                        It doesn't work.
                        I register my dataModelClass like this:
                        qmlRegisterUncreatableType<ClassNameOfModel>("com.test.application", 1, 0, "ClassNameOfModel", "no no no");
                        As soon as I start my application I see the output:

                        ...
                        qrc:/ConfigEdit.qml:3 Type ConfigEditEditForm unavailable
                        qrc:/ConfigEditEditForm.ui.qml:6 no no no
                        
                        p3c0P B 2 Replies Last reply
                        0
                        • T themts

                          @themts:
                          It doesn't work.
                          I register my dataModelClass like this:
                          qmlRegisterUncreatableType<ClassNameOfModel>("com.test.application", 1, 0, "ClassNameOfModel", "no no no");
                          As soon as I start my application I see the output:

                          ...
                          qrc:/ConfigEdit.qml:3 Type ConfigEditEditForm unavailable
                          qrc:/ConfigEditEditForm.ui.qml:6 no no no
                          
                          p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #12

                          @themts Can you post the complete compilable example ? Getting confused now O.o

                          157

                          1 Reply Last reply
                          0
                          • T themts

                            @themts:
                            It doesn't work.
                            I register my dataModelClass like this:
                            qmlRegisterUncreatableType<ClassNameOfModel>("com.test.application", 1, 0, "ClassNameOfModel", "no no no");
                            As soon as I start my application I see the output:

                            ...
                            qrc:/ConfigEdit.qml:3 Type ConfigEditEditForm unavailable
                            qrc:/ConfigEditEditForm.ui.qml:6 no no no
                            
                            B Offline
                            B Offline
                            Buttink
                            wrote on last edited by
                            #13

                            @themts This is a known problem with registering an uncreateable type. Its because QML is declarative. See this ticket https://bugreports.qt.io/browse/QTBUG-36752?jql=text ~ "Uncreatable C%2B%2B type can't be used for QML property types" . You have to just do QmlRegisterType<>. If you read the docs for qmlRegisterUncreatableType it says "This is useful where the type is only intended for providing attached properties or enum values.".

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              themts
                              wrote on last edited by themts
                              #14

                              @Buttink:
                              I already tried qmlRegisterType<ClassNameOfModel>() but then I get the message:
                              ClassNameOfModel is not a type

                              A workaround for me was now to declare a generic property var.
                              This way I have no code-completition but it works. Of course I would prefer a typesafe way.

                              I think it only works if I register the class as a creatable object, but then I have to implement a copyconstructor etc.

                              B 1 Reply Last reply
                              0
                              • T themts

                                @Buttink:
                                I already tried qmlRegisterType<ClassNameOfModel>() but then I get the message:
                                ClassNameOfModel is not a type

                                A workaround for me was now to declare a generic property var.
                                This way I have no code-completition but it works. Of course I would prefer a typesafe way.

                                I think it only works if I register the class as a creatable object, but then I have to implement a copyconstructor etc.

                                B Offline
                                B Offline
                                Buttink
                                wrote on last edited by
                                #15

                                @themts Hmmm that sounds really odd, but without the code, I don't know why it would output that error. I will say that you do not need a copy constructor. You could make a new qml type from

                                class Thing : public QObject
                                {
                                    Q_OBJECT
                                public:
                                    explicit Thing(QObject* parent = nullptr);
                                    virtual ~Thing();
                                };
                                
                                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