私は私ののpython3/PyQt5(5.6)コードからpyqtSlot
飾ら方法にQMLListView
からactivated
信号を接続したいと思います。PyQt5 pyqtSlotをQML ListView信号に接続するにはどうすればよいですか?
私の現在のアプローチは、QQmlApplicationEngine
を通してQMLシーンをロードしてからfindChild()
を使用して、ListView
への参照を取得することです。
問題は、findChild(QObject, 'myList')
のようなQObjectを検索するときにListViewしか見つからないということです。しかし、htisオブジェクトはactivated
シグナルにアクセスできません。このシグナルはQAbstractItemView
とその子孫に対してのみ定義されているからです。
したがって、私がfindChild(QListView, 'myList')
を試してみると、結果はNone
です。そのため私はactivated
信号に到達できません。これはPyQt5のバグですか、あるいはこの信号に接続する別の方法ですか?
ここでは、最小限の動作例を示します。
list.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_())
List.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"
}
}
}
}