QObject::findChild() 在没有明显原因的情况下返回 None [英] QObject::findChild() returns None without obvious reason

查看:67
本文介绍了QObject::findChild() 在没有明显原因的情况下返回 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Qt Quck 和 Qt5/PyQt 的新手,现在我遇到了一个奇怪的问题.我试图在下面的 QML 定义中找到一个 objectName "test" 的对象,如下所示:

I'm new to Qt Quck and Qt5/PyQt, and now I've faced a strange problem. I'm trying to find an object with objectName "test" in the below QML definition like this:

self.rootObject().findChild(QObject, "test")

但是调用返回 None.但是,如果我将 objectName: "test" 属性移动到父 Tab 元素,则它已成功找到.它只是在子 Item 中找不到.同样,findChild() 也找不到 addChannelmodifyChannelremoveChannel 对象.

But the call returns None. However, if I move the objectName: "test" property to the parent Tab element, then it's found successfully. It's only not found whem inside the child Item. Similarly, addChannel, modifyChannel and removeChannel objects are also not found by findChild().

import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.1
import "TouchStyles"

Item {
    ListModel { }

    TouchButtonFlatStyle { id: touchButtonFlat }
    TouchTabViewStyle { id: touchTabView }

    Rectangle {
        width: 480
        height: 230

        TabView {
            currentIndex: 0
            tabPosition: 1
            anchors.fill: parent
            style: touchTabView

            Tab {
                title: "Play"

                Item {
                    anchors.fill: parent
                    PianoKeyboard { anchors.centerIn: parent }
                }
            }
            Tab {
                title: "Channels"
                Item {
                    objectName: "test"
                    ListView {
                        anchors.fill: parent
                        model: listModel
                        delegate: Channel {}
                    }
                    BorderImage {
                        border.bottom: 8
                        anchors.bottom: parent.bottom
                        source: "images/toolbar.png"
                        width: parent.width
                        height: 50

                        RowLayout {
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.horizontalCenter: parent.horizontalCenter

                            Button { text: "Add"; objectName: "addChannel" }
                            Button { text: "Modify"; objectName: "modifyChannel" }
                            Button { text: "Remove"; objectName: "removeChannel" }
                        }
                    }
                }
            }
        }
    }
}

我做错了什么?Qt 文档说搜索是递归执行的.为什么不遍历整个对象树?

What am I doing wrong? The Qt documentation says that the search is performed recursively. Why doesn't it traverse the entire object tree?

推荐答案

该问题与选项卡仅根据需要实例化"这一事实有关.第一个选项卡总是被实例化,所以如果你把 objectName 放在那里,它就会被找到.

The problem is related to the fact that tabs are "instantiated" only on demand. The first tab is always instantiated, so if you put the objectName there it will be found.

仅当您实例化第二个选项卡(选择它)时,才会在第二个选项卡中找到它.类似地,在 TabView 上使用 findChild 可能会实例化每个选项卡(因为它正在寻找它们),因此之后 findChild 工作即使第二个选项卡没有被选中.

It will be found in the second tab only if you instantiate the second tab (select it). Similarly, using findChild on the TabView probably instantiates each tab (since it looking for them), so after that a findChild works even if second tab was not selected.

结论:首先实例化所有选项卡(在 TabView 上执行 findChild 是一种方法,但可能是一种黑客行为),然后执行 findChild> 为项目.

Conclusion: instantiate all tabs first (doing a findChild on the TabView is one way but may be a hack), then do the findChild for the item.

这篇关于QObject::findChild() 在没有明显原因的情况下返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆