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. How to pass object id in a property?
Forum Updated to NodeBB v4.3 + New Features

How to pass object id in a property?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 5.0k 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.
  • S Offline
    S Offline
    spaluc
    wrote on last edited by
    #1

    Hi,

    I would like to make new QML component, let say a Button that push on the PageStack a page, I want the Page to be specified in Button property:
    @
    Button {
    property string target
    id: button1
    x: 104
    y: 230
    text: "Button"
    onClicked: {
    pageStack.push(target)
    }

        }
    

    @
    The property target is supposed to be set to a target Page id in Qt Creator. The intent is to type in everything in Design mode without need to fill in onClicked. How to use id stored in a property?

    thanks

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Ids don't work as properties, they are not visible in C++. You can make the property into a var (or variant), and assign the object to it by [removed]
      @
      property var target: someObjectId
      @

      If you need access in C++, you will have to add objectName string and use QObject::findChild().

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        spaluc
        wrote on last edited by
        #3

        I do not want to access it in C++, I want to access it in QML through property, I want the property to be set in Design mode and pass to push (see line 9) function so that my button is fully configurable in design mode.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Well, does the var property not work?

          (Z(:^

          1 Reply Last reply
          0
          • S Offline
            S Offline
            spaluc
            wrote on last edited by
            #5

            variant property is not visible in Design mode, Qt Creator does not display properties of variant type in Property Editor.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              chriadam
              wrote on last edited by
              #6

              The id attribute is a very special attribute. It is not a property, and certain special restrictions apply to it. See the "id attribute documentation":http://doc-snapshot.qt-project.org/5.0/qtqml-syntax-objectattributes.html#the-id-attribute for more information on the topic.

              Basically, whenever you use an id in a JavaScript expression in QML, it resolves to a QObject*.

              So, you can create a property which can store what the id resolves to, but you cannot store the id itself.

              Example:
              @
              Item {
              id: root
              property Item childItem: child

              Item {
                  id: child
              }
              

              }
              @

              Similarly, you can call functions which have a QObject* argument, and "pass" an id as the parameter (since internally, the id gets resolved to a QObject*).

              I hope this answers your question, since I'm not entirely certain what you're hoping to achieve.

              Cheers,
              Chris.

              /edit:
              You can use "QtObject" as the most basic QObject* property type instead of Item, also, if the object isn't a QDeclarativeItem or QQuickItem derived type.

              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