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. Qml property custom type without creation
Forum Updated to NodeBB v4.3 + New Features

Qml property custom type without creation

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 2.1k 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.
  • L Offline
    L Offline
    luke_dirtwalker
    wrote on last edited by
    #1

    Hi,

    I have a c++ class which i want to use in qml and in other c++ classes;

    Say C++ class TimeHandler; and a class UiHandler;

    I register TimeHandler as a qmlType.

    In the class UiHandler I create 2 instances of TimeHandler and call
    @
    setContextProperty("thandler1",&timeHandlerObj);
    setContextProperty("thandler2",&timeHandlerObj);
    @

    now i have a qml component:
    @
    Item{
    property TimeHandler timeHandler;

    //some code which uses timeHandler and calls TimeHandler functions
    }
    @

    this Item I use in two Pages and there I set:

    @
    DateItem{
    timeHandler: thandler1
    }
    @

    and in the other Page thandler2

    but the Problem I have is that the qml-code: property TimeHandler already creates an Object (because if I try to use qmlRegisterUncreatableType I get an error in qml.)

    Is there a way to set those timeHandlers without creating unused Objects ?

    Hope you can understand my question :)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      Hi,

      A couple of things:
      @
      Item {
      property Rectangle someRectangle: null // shouldn't result in the instantiation of a Rectangle
      }
      @

      So you should be able to just do as you did above, without it constructing an extra one. Just don't initialise the property, or initialise it to null.

      However, there is another way: declare a QtObject property instead of a TimeHandler property, and then register the TimeHandler type as an uncreatable type.

      You'll still be able to access all of the functions and properties from QML, although bindings will be slower since the type information isn't available at compile time.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luke_dirtwalker
        wrote on last edited by
        #3

        Thanks I tried to do it with null as default value. But if I register TimeHandler as an UncreatableType it still fails in qml at the property.

        Could you please explain the other way you stated a little bit in more detail? I can't follow.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chrisadams
          wrote on last edited by
          #4

          If TimeHandler is registered as an UncreatableType, you cannot use TimeHandler as a property type; you should use QtObject as the property type instead.

          If TimeHandler is registered as a normal creatable type, you can use TimeHandler as a property type, and a property initialisation like "property TimeHandler th: null" should not result in an instance of TimeHandler being created.

          Cheers,
          Chris.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luke_dirtwalker
            wrote on last edited by
            #5

            Oh I see, now I understand. Thanks a lot for your help.

            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