How to pass object id in a property?



  • 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


  • Moderators

    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().



  • 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.


  • Moderators

    Well, does the var property not work?



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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.