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. Custom QML Type from C++ with constructor with parameters. How?

Custom QML Type from C++ with constructor with parameters. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 2.5k 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.
  • B Offline
    B Offline
    bogong
    wrote on 18 Sept 2020, 07:54 last edited by bogong
    #1

    Hello all!
    Is there any way to create Custom QML Type with parameters in constructor? For now I found only solution to run function on Component.onCompleted. Something like this:

    CustomQmlType {
    
        ...
        Component.onCompleted: {
             init(Param1,Param2);
        }
    }
    

    where function init implemented in C++:

    public slots:
    
    	void init(QVariant Param1,QVariant Param2);
    

    Is there any way to set parameters at time of running constructor?

    S 1 Reply Last reply 18 Sept 2020, 09:00
    0
    • B bogong
      18 Sept 2020, 07:54

      Hello all!
      Is there any way to create Custom QML Type with parameters in constructor? For now I found only solution to run function on Component.onCompleted. Something like this:

      CustomQmlType {
      
          ...
          Component.onCompleted: {
               init(Param1,Param2);
          }
      }
      

      where function init implemented in C++:

      public slots:
      
      	void init(QVariant Param1,QVariant Param2);
      

      Is there any way to set parameters at time of running constructor?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 18 Sept 2020, 09:00 last edited by
      #2

      @bogong said in Custom QML Type from C++ with constructor with parameters. How?:

      Is there any way to set parameters at time of running constructor?

      No, there is no syntax for this. QML always instantiates using the default constructor.

      What you can do, is to create the object dynamically via JS: there you can specify starting parameters (see https://doc.qt.io/qt-5/qml-qtqml-component.html#createObject-method). But it's the same as just setting some properties on the object in your example, so I doubt it helps you.

      Other than that, there are some workarounds:

      • introduce a singleton holding your params - then in constructor of your CustomQmlType, get parameter values from that singleton
      • if you are OK with storing your QML type as a property of another type, you can always expose some C++ factory for your CustomQmlType objects, which will accept parameters

      (Z(:^

      B 1 Reply Last reply 18 Sept 2020, 09:15
      3
      • S sierdzio
        18 Sept 2020, 09:00

        @bogong said in Custom QML Type from C++ with constructor with parameters. How?:

        Is there any way to set parameters at time of running constructor?

        No, there is no syntax for this. QML always instantiates using the default constructor.

        What you can do, is to create the object dynamically via JS: there you can specify starting parameters (see https://doc.qt.io/qt-5/qml-qtqml-component.html#createObject-method). But it's the same as just setting some properties on the object in your example, so I doubt it helps you.

        Other than that, there are some workarounds:

        • introduce a singleton holding your params - then in constructor of your CustomQmlType, get parameter values from that singleton
        • if you are OK with storing your QML type as a property of another type, you can always expose some C++ factory for your CustomQmlType objects, which will accept parameters
        B Offline
        B Offline
        bogong
        wrote on 18 Sept 2020, 09:15 last edited by
        #3

        @sierdzio Thx a lot. Issue closed.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 18 Sept 2020, 09:30 last edited by GrecKo
          #4

          What can be usefull is to inherit from QQmlParserStatus and defer the logic of your component untill componentComplete is called, you'll be sure all the property bindings have been evaluated.

          R 1 Reply Last reply 3 Jun 2024, 02:52
          2
          • G GrecKo
            18 Sept 2020, 09:30

            What can be usefull is to inherit from QQmlParserStatus and defer the logic of your component untill componentComplete is called, you'll be sure all the property bindings have been evaluated.

            R Offline
            R Offline
            RubbishZhYic
            wrote on 3 Jun 2024, 02:52 last edited by
            #5

            @GrecKo I found a way similar to yours, but IMO it may be better to do it at classBegin, so that you can use group property binding syntax sugar in qml.
            parameters can be passed in the C++ side via QQmlContext's contextProperty, and accessed in classBegin via this context hierachy.

            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