Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    declare property of pointer in qml

    QML and Qt Quick
    property
    3
    15
    6073
    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.
    • T
      themts last edited by 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

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @themts last edited by

        @themts What is datamodel ?

        157

        1 Reply Last reply Reply Quote 0
        • T
          themts last edited by

          datamodel is a c-class.

          p3c0 1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators @themts last edited by

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

            157

            1 Reply Last reply Reply Quote 0
            • T
              themts last edited by 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?

              p3c0 1 Reply Last reply Reply Quote 0
              • p3c0
                p3c0 Moderators @themts last edited by

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

                157

                1 Reply Last reply Reply Quote 0
                • T
                  themts last edited by

                  ???

                  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]
                      }
                  
                  p3c0 1 Reply Last reply Reply Quote 0
                  • p3c0
                    p3c0 Moderators @themts last edited by

                    @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 Reply Quote 0
                    • T
                      themts last edited by

                      This declaration would create an instance of ClassNameOfModel.

                      p3c0 1 Reply Last reply Reply Quote 0
                      • p3c0
                        p3c0 Moderators @themts last edited by

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

                        157

                        1 Reply Last reply Reply Quote 0
                        • T
                          themts last edited by

                          @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
                          
                          p3c0 B 2 Replies Last reply Reply Quote 0
                          • p3c0
                            p3c0 Moderators @themts last edited by

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

                            157

                            1 Reply Last reply Reply Quote 0
                            • B
                              Buttink @themts last edited by

                              @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 Reply Quote 0
                              • T
                                themts last edited by 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 1 Reply Last reply Reply Quote 0
                                • B
                                  Buttink @themts last edited by

                                  @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 Reply Quote 0
                                  • First post
                                    Last post