我想将来自QMLactivated
的信号连接到来自我的Python3/PyQt5 (5.6) 代码的装饰方法。 ListView
pyqtSlot
我目前的方法是在我的代码中加载 QML 场景,QQmlApplicationEngine
然后使用它findChild()
来获取对我的ListView
.
问题是,我只能在搜索 QObject 时找到 ListView findChild(QObject, 'myList')
。但是 htis 对象没有让我访问activated
信号,很可能是因为这个信号只是为QAbstractItemView
它的后代定义的。
所以如果我尝试findChild(QListView, 'myList')
结果是None
. 因此,我无法收到activated
信号。这是 PyQt5 中的一个错误,还是有另一种方法可以让我连接到这个信号?
这是一些最小的工作示例。
列表.py:
import sys
from OpenGL import GL
from PyQt5.QtCore import QUrl, QObject
from PyQt5.QtWidgets import QApplication, QListView
from PyQt5.QtQml import QQmlApplicationEngine
# Main Function
if __name__ == '__main__':
# Create main app
app = QApplication(sys.argv)
# Create QML engine
engine = QQmlApplicationEngine(app)
# Load the QML scene from file
engine.load(QUrl('List.qml'))
for root in engine.rootObjects():
node = root.findChild(QListView, 'myList')
if node:
# At this point I would like to connect something to the
# node.activated signal
print(node)
# Execute the application and exit
sys.exit(app.exec_())
列表.qml:
import QtQuick 2.0
import QtQuick.Window 2.2
Window {
visibility: Window.FullScreen
visible: true
ListView {
objectName: "myList"
anchors.fill: parent
delegate: Item {
width: parent.width * 0.8
height: 40
Row {
id: row1
Rectangle {
width: 40
height: 40
color: colorCode
}
Text {
text: name
font.bold: true
anchors.verticalCenter: parent.verticalCenter
}
spacing: 10
}
}
model: ListModel {
ListElement {
name: "Grey"
colorCode: "grey"
}
ListElement {
name: "Red"
colorCode: "red"
}
ListElement {
name: "Blue"
colorCode: "blue"
}
ListElement {
name: "Green"
colorCode: "green"
}
}
}
}