QObject::findChild() 在没有明显原因的情况下返回 None

编程入门 行业动态 更新时间:2024-10-28 14:24:33
本文介绍了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() 也找不到 addChannel、modifyChannel 和 removeChannel 对象.

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

本文发布于:2023-11-24 01:20:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623457.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   原因   QObject   findChild

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!