Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

c++ - How to access objectNames of objects in ApplicationWindow of QML?

How to access objectName of children of ApplicationWindow?

With ApplicationWindow:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

ApplicationWindow {
    id: head
    visible: true
    width: 640
    height: 480

    Rectangle{ height:1; width: 1; objectName: "AA"}
    Rectangle{ height:1; width: 1; objectName: "BB"}

    Component.onCompleted:
    {
        console.log("parent.data[0].objectName", head.data[0].objectName)
        console.log("parent.data[1].objectName", head.data[1].objectName)
    }
}

Output:

qml: parent.data[0].objectName ApplicationWindow
qml: parent.data[1].objectName 

With Window:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    id: head
    visible: true
    width: 640
    height: 480

    Rectangle{height:1; width: 1; objectName: "AA"}
    Rectangle{height:1; width: 1; objectName: "BB"}

    Component.onCompleted:
    {
        console.log("parent.data[0].objectName", head.data[0].objectName)
        console.log("parent.data[1].objectName", head.data[1].objectName)
    }
}

Output:

qml: parent.data[0].objectName AA
qml: parent.data[1].objectName BB
question from:https://stackoverflow.com/questions/65952407/how-to-access-objectnames-of-objects-in-applicationwindow-of-qml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of data, we have to access contentData with respect to ApplicationWindow

Component.onCompleted:
    {
        console.log("parent.data[0].objectName", head.contentData[0].objectName)
        console.log("parent.data[1].objectName", head.contentData[1].objectName)
    }

From: https://doc.qt.io/qt-5/qml-qtquick-controls2-applicationwindow.html#contentData-prop

contentData : list

This default property holds the list of all objects declared as children of the window.

The data property allows you to freely mix visual children, resources and other windows in an ApplicationWindow.

If you assign an Item to the contentData list, it becomes a child of the window's contentItem, so that it appears inside the window. The item's parent will be the window's contentItem.

It should not generally be necessary to refer to the contentData property, as it is the default property for ApplicationWindow and thus all child items are automatically assigned to this property.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...