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

PySide6中QML TableView无法显示QStandardItemModel问题排查

问题排查与修复方案

你的代码存在几个关键问题导致TableView无法显示数据,以下是具体问题和修复步骤:

1. QML TableView未定义列配置

QtQuick.Controls的TableView需要明确声明列信息,否则无法渲染列内容,原代码缺少列的定义逻辑。

2. Python端Model重复创建

log属性的getter方法每次被调用时都会新建一个QStandardItemModel实例,且未保留引用,会导致Model被垃圾回收或数据绑定不稳定。

修复后的代码

修改后的main.py

QML_IMPORT_NAME = "com.simplified"
QML_IMPORT_MAJOR_VERSION = 1

from pathlib import Path
import sys

from PySide6.QtCore import QObject, Signal, Property, Qt
from PySide6.QtGui import QGuiApplication, QStandardItemModel, QStandardItem
from PySide6.QtQml import QQmlApplicationEngine, QmlElement, QmlSingleton

LOG_ENTRIES = [
    {
        "Timestamp": "2024-07-01 19:16:03.326",
        "Name": "root.child",
        "Level": "DEBUG",
        "Message": "This is a debug message",
    },
    {
        "Timestamp": "2024-07-01 19:16:03.326",
        "Name": "root.child",
        "Level": "INFO",
        "Message": "This is an info message",
    },
]

FIELD_NAMES = ["Timestamp", "Name", "Level", "Message"]

@QmlElement
@QmlSingleton
class Simplified(QObject):
    log_changed = Signal()

    def __init__(self) -> None:
        super().__init__()
        # 初始化时创建一次Model,避免重复实例化
        self._log = QStandardItemModel(len(LOG_ENTRIES), len(FIELD_NAMES))
        self._log.setHorizontalHeaderLabels(FIELD_NAMES)
        for line in LOG_ENTRIES:
            row = [QStandardItem(str(line[key])) for key in FIELD_NAMES]
            self._log.appendRow(row)

    @Property(QStandardItemModel, notify=log_changed)
    def log(self):
        return self._log

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

    qml_file = Path(__file__).resolve().parent / "qml" / "Main.qml"
    engine.load(qml_file)

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(application.exec())

修改后的Main.qml

import QtQuick
import QtQuick.Window
import QtQuick.Controls
import com.simplified

Window {
    width: 740
    height: 540
    visible: true
    title: "Python log viewer"

    TableView {
        id: log

        anchors.fill: parent

        columnSpacing: 1
        rowSpacing: 1
        clip: true

        model: Simplified.log

        // 为每个字段定义对应列
        TableViewColumn {
            role: "0"
            title: "Timestamp"
            width: 180
        }
        TableViewColumn {
            role: "1"
            title: "Name"
            width: 120
        }
        TableViewColumn {
            role: "2"
            title: "Level"
            width: 80
        }
        TableViewColumn {
            role: "3"
            title: "Message"
            width: 340
        }

        delegate: Rectangle {
            border.width: 1
            width: styleData.columnWidth
            height: 30

            Text {
                text: styleData.value
                anchors.centerIn: parent
                elide: Text.ElideRight
            }
        }
    }
}

关键修复点说明

  • Model复用:在Simplified类初始化时创建并保存Model实例,确保每次访问log属性返回同一个对象,避免重复创建导致的数据绑定失效。
  • TableView列配置:添加TableViewColumn,通过role指定对应Model的列索引(QStandardItemModel的列对应role为字符串形式的索引),同时设置列宽和标题。
  • 委托优化:使用styleData.value获取对应列的数据,替代原有的display,并设置委托的固定高度和自适应宽度,确保内容正确显示。
  • 移除冗余代码:删除engine.singletonInstance()调用,@QmlSingleton和@QmlElement已自动完成单例注册,加载QML时会自动实例化。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 17:02:20