You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据父ListView模型的值定义对应子ListView模型

问题:ECU诊断应用中QML子ListView无法按ECU加载对应故障码模型

我正在开发一款车辆电子控制单元(ECU)诊断应用,使用Python和QML实现ECU故障码读取模块。模块采用两层ListView结构:父ListView展示包含ECU名称的容器,每个容器内的子ListView对应展示该ECU的故障码列表。

目前遇到的问题是:无法让每个子ListView根据父ListView中对应的ECU名称加载专属的故障码模型,所有子ListView当前都使用同一个通用模型,导致显示内容一致。我曾尝试为故障码模型添加ECU名称参数,但无法从QML中传递参数,出现参数缺失错误。

Python代码

from PyQt6.QtCore import QObject, pyqtSlot, pyqtSignal, pyqtProperty

class DTCReading(QObject):
    def __init__(self):
        QObject.__init__(self)
        # these lists will change depending on the response of ECU, now for ease of debugging they are defined statically
        self.ecu_list = ['uBCM', 'ACU', 'ESCL']
        self.dtc_list_uBCM = ['p1230', 'u1100', 'f1441']
        self.dtc_list_ACU = ['a1111', 'b2222', 'c3333']
        self.dtc_list_ESCL = ['a1330', 'u0008', 'p1856']

    # signals
    dtc_list_emmition_signal = pyqtSignal()
    ecu_list_emmition_signal = pyqtSignal()


    # ECU list model for parent ListView
    @pyqtProperty(list, notify=ecu_list_emmition_signal)
    def ecu_list_model(self):
        return self.ecu_list 

    def ecu_list_emmition(self):
        self.ecu_list_emmition_signal.emit()

   
    # DTC list model for child ListView 
    @pyqtProperty(list, notify=dtc_list_emmition_signal)
    def dtc_list_model(self):
        return self.dtc_list_uBCM # this list-model should be different depending on the type of ECU from ECU list model

    @pyqtSlot()
    def dtc_list_emmition(self):
        self.dtc_list_emmition_signal.emit()

QML代码

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Window


Item {
    id: dtcReadingRootItem

    Rectangle {
        id: rectangle
        color: "#2c2c2c"
        border.width: 0
        anchors.fill: parent

        //parent ListView with ECU names and child ListView inside
        ListView {
            id: ecuListView
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.topMargin: 110
            anchors.bottomMargin: 10
            anchors.rightMargin: 10
            anchors.leftMargin: 10
            clip: true
            spacing: 5
            interactive: true
            Component.onCompleted: {dtc_reading_connection.dtc_reading()}

            delegate: Rectangle {
                id: ecuDelegate
                height: dtcListView.model.length * 25 + 30
                color: "#383838"
                border.color: "#fff100"
                border.width: 1
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.rightMargin: 20
                anchors.leftMargin: 0

                Label {
                    id: ecuName
                    width: 150
                    color: "#ffffff"
                    text: model.modelData
                    anchors.top: parent.top
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.topMargin: 5
                    styleColor: "#ffffff"
                    font.pointSize: 10
                    font.family: "Verdana"
                    renderType: Text.NativeRendering
                }
                
                //child ListView with error codes
                ListView {                  
                    id: dtcListView
                    height: model.length * 25
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.top: parent.top
                    anchors.topMargin: 25
                    anchors.rightMargin: 20
                    anchors.leftMargin: 20
                    clip: true
                    spacing: 5
                    interactive: true
                    Component.onCompleted: {dtc_reading_connection.dtc_reading()}


                    delegate: Rectangle {
                        id: dtcDelegate
                        height: 20
                        visible: true
                        color: "#fff100"
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.rightMargin: 0
                        anchors.leftMargin: 0
                        width: dtcListView.width-40


                        Label {
                            id: testLabel
                            width: 150
                            color: "#000000"
                            text: model.modelData
                            anchors.left: parent.left
                            anchors.right: parent.right
                            anchors.top: parent.top
                            anchors.bottom: parent.bottom
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            anchors.rightMargin: 20
                            anchors.leftMargin: 20
                            anchors.bottomMargin: 20
                            anchors.topMargin: 20
                            styleColor: "#ffffff"
                            font.pointSize: 12
                            font.family: "Verdana"
                            renderType: Text.NativeRendering
                        }
                    }

                    model: {dtc_reading_connection.dtc_list_model} //I don't know how to define here different models depending on ECU name from parent ListView model data

                    ScrollBar.vertical: ScrollBar {}
                }
            }

            model: {dtc_reading_connection.ecu_list_model} //parent ListView model with ECU names

            ScrollBar.vertical: ScrollBar {}
        }
    }
}

尝试的错误代码

@pyqtProperty(list, notify=dtc_list_emmition_signal)
def dtc_list_model(self, ecu):
    if ecu == 'uBCM':
        return self.dtc_list_uBCM
    else:
        return self.dtc_list_ACU

内容的提问来源于stack exchange,提问作者Alexander Poverennov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 00:10:02