Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access Signal Handlers of Defined Properties

Access Signal Handlers of Defined Properties

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.4k 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.
  • K Offline
    K Offline
    Korchkidu
    wrote on last edited by
    #1

    Hi,

    I have C++ custom types exposed to QML as D1, D2, D3, etc. All of them inherit from an abstract base class ABC. I want to implement a generic QML component with a property containing any class inheriting from ABC and able to use D1/D2/D3 properties (only those inherited from ABC):

    // MyGenericView.qml
    @Rectangle
    {
    property CustomClass myCustomObject
    property alias customProperty: myCustomObject.property // <<== ERROR

    onCustomPropertyChanged: // <<== ERROR too
    {
    console.log("YOUHOU")
    }
    }
    @

    Any idea what I should do?

    Thanks.

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hi, you should create resource instances and not properties, that is at least what I would do for c++ objects in QML. So instead of "property CustomClass myCustomObject" try a standalone object
      @
      CustomClass {
      id: myCustomObject
      onCustomPropertyChanged: ...
      }
      @
      that should work better and you can access it via the id, if you want to use it as a property I don't know if you can access the internal properties of another property (that is what you are trying to do).

      Btw this thread should go in the Qt Quick sub forum I guess :)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Korchkidu
        wrote on last edited by
        #3

        Thanks for your answer. But my container needs somehow to be generic, like running the "run" function of its attached CustomObject. With this solution, I will have to maintain one QML Wrapper for each of my custom object right?

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          I don't know what you mean, in your example you had a separate object for every Rectangle (it was just initialized with a property rather than a QML resource)?

          In QML you can also make use of inheritance like in c++, just put your Rectangle and the "CustomClass" object inside in a separate QML file and use that, then you only have to create objects ob that new file rather than doing the same thing if you need that multiple times.

          I hope you know what I mean, if not you can look at some QML examples maybe.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Korchkidu
            wrote on last edited by
            #5

            Thanks for your answer. I am not sure to understand what you mean actually.

            Suppose I have 100 CustomObject classes (like D1, D2, D3, etc) all inheriting from CustomObjectBaseClass. I don't want to do 100 QML item wrapping them. Ideally, I would like 1 QML item having a property to which I can attach any kind of CustomObjectD*. This QML item would be responsible to call the run function of the attached CustomObject.

            So based on you solution, I could maybe do something like:

            @// genericContainer.qml
            import myCustomObjectsLib 1.0
            Rectangle
            {
            <generic visual stuff here>
            }
            @

            @// myCustomObjectD1.qml
            import myCustomObjectsLib 1.0

            GenericContainer
            {
            CustomObjectD1{id: tool}

            function run()
            {
            tool.Run()
            }
            onTriggered: run()
            }
            @

            But, my main concern is that in this case, I will have to duplicate all "tool-related" code in each individual myCustomObjectD*.qml files which will get quickly impractical. So if possible, I would like to refactor all this by doing something like:

            @// genericContainer.qml
            import myCustomObjectsLib 1.0
            Rectangle
            {
            <generic visual stuff here>

            property CustomObjectBaseClass tool;
            

            function run()
            {
            tool.Run()
            }
            onTriggered: run()
            }
            @

            or something similar. Just note that I just begun with Qt / QML so I am still experimenting here and there;) Pardon me if I am slow to understand;)

            Best regards.

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on last edited by
              #6

              Well I don't know but your example should work, what is the problem with that?
              In your first post you wanted something different and that is not possible with a property in QML. so maybe you need to provide a better example what you really want to do or where your "tools" are coming from etc. are they all defined in your "myCustomObjectsLib" (c++ classes I assume)?

              But in general you can either use a property or a standalone object, but if you don't know then class to instantiate, you can only use a property similar to your example and inject the correct class later.

              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