为什么在 Qt QML 中出现“TypeError: Value is undefined and could not be converted to an object”?

问题描述 投票:0回答:1

我在Qt Qml中做了一个简单的listview。我正在使用 PySide2 和 QStringListModel 来填充列表视图。当像这个简单示例中的列表更大时,在 Qt Qml 中单击 printBtn(应将文本颜色更改为绿色)会出现以下错误:“TypeError:值未定义,无法转换为对象”如果列表包含的元素较少,似乎没问题。如何更改我的代码以防止将来出现此问题。

这可能是 Qt 中的错误吗?有什么解决方法吗?

请参阅下面我的简单代码片段:

蟒蛇:

import sys
from PySide2.QtCore import Qt, QStringListModel, QObject, Signal, Slot
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine

class Main(QObject):

    clicked = Signal()

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

    @Slot()
    def justClicking(self):
        self.clicked.emit()

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    main = Main()
    engine.rootContext().setContextProperty("main", main)

    data = ["London", "New York", "Paris", "Tokyo", "Sydney", "Madrid", "Mumbai", "Istanbul", "Bangkok", "Moscow", "Toronto", "Rome", "Shanghai", "Rio de Janeiro", "Seoul", "Berlin", "Cape Town", "Amsterdam", "Los Angeles", "San Francisco", "Cairo", "Barcelona", "Buenos Aires", "Melbourne", "Athens", "Vancouver", "Dubai", "Prague", "Edinburgh", "Singapore"]

    model = QStringListModel()
    model.setStringList(data)

    context = engine.rootContext()
    context.setContextProperty("myModel", model)

    engine.load("main.qml")
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

Qml代码:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15

Window {
    visible: true
    color: "#161515"
    width: 640
    height: 480
    title: qsTr("Listview Example")

    Rectangle {
        id: rectangle
        x: 51
        y: 63
        width: 539
        height: 143
        color: "#ffffff"

        ListView {
            id: listView
            anchors.fill: parent
            model: myModel
            currentIndex: 0
            delegate: Rectangle {
                id: dlg
                width: listView.width
                height: 25
                color: index === listView.currentIndex ? "lightgrey" : "#00000000"
                radius: 10

                property alias colorOfText: titleText.color

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        listView.currentIndex = index
                    }
                }
                Text {
                    id: titleText
                    text: model.display
                    font.pixelSize: 18
                    color: "black"
                    padding: 5
                }
                Connections {
                    target: main
                    function onClicked() {
                        var myListItemObjectC = listView.contentItem.children[listView.currentIndex]
                        myListItemObjectC.colorOfText = "green"
                    }
                }
            }
        }
    }
    Button {
        id: printBtn
        x: 286
        y: 241
        width: 69
        height: 35
        text: qsTr("Print")
        onClicked: {
            main.justClicking()
        }
    }
}

我一直试图不使用 QStringListModel,试图将它们附加到 ListModel,但没有任何运气。感谢所有帮助!

python qt pyqt qml pyside2
1个回答
0
投票

我将你的应用程序移植到 PySide6。它似乎只需要最少的代码更改就可以工作。

这里是 PySide2 引用更改为 PySide6 的

main.py

import sys
from PySide6.QtCore import Qt, QStringListModel, QObject, Signal, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine

class Main(QObject):

    clicked = Signal()

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

    @Slot()
    def justClicking(self):
        self.clicked.emit()

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    main = Main()
    engine.rootContext().setContextProperty("main", main)

    data = ["London", "New York", "Paris", "Tokyo", "Sydney", "Madrid", "Mumbai", "Istanbul", "Bangkok", "Moscow", "Toronto", "Rome", "Shanghai", "Rio de Janeiro", "Seoul", "Berlin", "Cape Town", "Amsterdam", "Los Angeles", "San Francisco", "Cairo", "Barcelona", "Buenos Aires", "Melbourne", "Athens", "Vancouver", "Dubai", "Prague", "Edinburgh", "Singapore"]

    model = QStringListModel()
    model.setStringList(data)

    context = engine.rootContext()
    context.setContextProperty("myModel", model)

    engine.load("main.qml")
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

这是用 Qt6 导入替换 Qt5 导入的

main.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Window

Window {
    visible: true
    color: "#161515"
    width: 640
    height: 480
    title: qsTr("Listview Example")

    Rectangle {
        id: rectangle
        x: 51
        y: 63
        width: 539
        height: 143
        color: "#ffffff"

        ListView {
            id: listView
            anchors.fill: parent
            model: myModel
            currentIndex: 0
            delegate: Rectangle {
                id: dlg
                width: listView.width
                height: 25
                color: index === listView.currentIndex ? "lightgrey" : "#00000000"
                radius: 10

                property alias colorOfText: titleText.color

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        listView.currentIndex = index
                    }
                }
                Text {
                    id: titleText
                    text: model.display
                    font.pixelSize: 18
                    color: "black"
                    padding: 5
                }
                Connections {
                    target: main
                    function onClicked() {
                        var myListItemObjectC = listView.contentItem.children[listView.currentIndex]
                        myListItemObjectC.colorOfText = "green"
                    }
                }
            }
        }
    }
    Button {
        id: printBtn
        x: 286
        y: 241
        width: 69
        height: 35
        text: qsTr("Print")
        onClicked: {
            main.justClicking()
        }
    }
}

这是您的应用程序运行的屏幕截图:

© www.soinside.com 2019 - 2024. All rights reserved.