我是Qt Quck和Qt5/PyQt的新手,現在我遇到了一個奇怪的問題。我想在下面的QML定義這樣來找到對象名「試驗」的對象:QObject :: findChild()返回None沒有明顯的原因
self.rootObject().findChild(QObject, "test")
但調用返回無。但是,如果我將objectName: "test"
屬性移動到父級Tab元素,則會成功找到它。這只是在孩子Item
內部找不到。同樣,addChannel
,modifyChannel
和removeChannel
物體也未被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文檔說,搜索是遞歸執行的。爲什麼它不遍歷整個對象樹?
最好的辦法是首先檢查搜索是否成功從C++。如果沒有,請將其報告爲Qt錯誤。否則,它會是一個PyQt錯誤。 –
'self.rootObject()。findChildren(QObject)'(即所有孩子)的結果是什麼? –
第一個標籤是否會發生同樣的情況(如果您將第二個標籤的Item複製到第一個標籤)?或者,如果您在執行findChild之前選擇第二個選項卡,那麼同樣的問題呢? – Schollii