python - TableView重新实现QAbstractTableModel后表格中没有数据显示
问题描述
我无法在我的 QML TableView 上显示数据。我已经定义了两个数组——我想在 TableView 上显示的标题和行,但到目前为止没有成功。下面是最小的可重现示例。
引擎.py
import os
import sys
from PySide2 import QtCore, QtGui, QtSql, QtQml
from Table import TbModel
from PySide2.QtWidgets import QApplication
if __name__ == "__main__":
current_dir = os.path.dirname(os.path.realpath(__file__))
app = QApplication(sys.argv)
QtQml.qmlRegisterType(TbModel, "TbModel", 1, 0, "TbModel")
engine = QtQml.QQmlApplicationEngine()
qml_path = os.path.join( "main.qml")
engine.load(QtCore.QUrl.fromLocalFile(qml_path))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
main.qml
import QtQuick 2.13
import QtQuick.Window 2.2
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls 2.13
import TbModel 1.0
ApplicationWindow {
visible: true
id: window
width: Screen.width
height: Screen.height
Grid {
width: 300
height: 100
visible: true
spacing: 200
TableView
{
id: idtable
model: TbModel { }
TableViewColumn {
role: "sci"
title: "sci"
}
TableViewColumn {
role: "year"
title: "year"
}
TableViewColumn {
role: "cont"
title: "cont"
}
// Component.onCompleted: {
// var roles = model.roleNameArray()
// for (var i = 0; i < model.columnCount(); i++)
// {
// var column = addColumn( Qt.createQmlObject(
// "import QtQuick.Controls 1.1; TableViewColumn {}",
// this) )
// column.role=roles[i]
// column.title=roles[i]
// }
// }
}
}
}
表.py
from PySide2.QtCore import QAbstractTableModel, QModelIndex, QObject, Qt
from PySide2 import QtCore
class TbModel(QAbstractTableModel):
def __init__(self, parent: QObject = None) -> None:
super().__init__(parent)
self.headers = ["sci", "year", "cont"]
self.rows = [("Newton", "1643-01-04", "Classical mechanics"),
("Einstein", "1879-03-14", "Relativity"),
("Darwin", "1809-02-12", "Evolution")]
def rowCount(self, parent=QModelIndex()):
return len(self.rows)
def columnCount(self, parent=QModelIndex()):
return len(self.headers)
def data(self, index, role):
if role != Qt.DisplayRole:
return None
return self.rows[index.row()][index.column()]
def headerData(self, section, orientation, role) :
if role != Qt.DisplayRole:
return None
if section < 0 or section >= len(self.headers):
return None
return self.headers[section]
def roleNames(self):
roles = {
Qt.UserRole + 1 : 'sci',
Qt.UserRole + 2 : 'year',
Qt.UserRole + 3 : 'cont'
}
return roles
@QtCore.Slot(result="QVariantList")
def roleNameArray(self):
names = []
names=self.headers
return names
我可以在 TableView 中选择行,但看不到数据。任何帮助表示赞赏。
解决方案
您的代码有 2 个错误:
在 C++
roleNames()
方法中返回 aQHash<int, QByteArray>
所以在 Python 中,您必须返回一个字典,其键是整数,值必须是 abytes
或QByteArray
,但在您的情况下,值是字符串。data 方法必须提供视图所需的信息,在这种情况下,它们是与“sci”、“year”和“cont”相关联的角色,其值为
Qt.UserRole + 1
,Qt.UserRole + 2
并且Qt.UserRole + 3
不同于Qt.DisplayRole
,但在您的逻辑中任何其他值不会Qt.DisplayRole
与上述内容相矛盾。QML 提供了几个 TableView:QtQuick.Controls 1.x和QtQuick。在您的情况下,您使用的第一个不需要 QAbstractTableModel 作为模型,而只需要 QAbstractListModel 因为视图使用的 QModelIndex 的列值始终为 1。
考虑到上述情况,您可能会遇到问题,因为 QML 提供了几个属于不同包的同名项目,为了解决这个问题,使用了命名空间。
考虑到上述情况,解决方案是:
from PySide2.QtCore import QAbstractListModel, QModelIndex, QObject, Qt, Slot
class TbModel(QAbstractListModel):
def __init__(self, parent: QObject = None) -> None:
super().__init__(parent)
self.headers = ["sci", "year", "cont"]
self.rows = [
("Newton", "1643-01-04", "Classical mechanics"),
("Einstein", "1879-03-14", "Relativity"),
("Darwin", "1809-02-12", "Evolution"),
]
def rowCount(self, parent=QModelIndex()):
return len(self.rows)
def data(self, index, role=Qt.DisplayRole):
row = index.row()
if 0 <= row < self.rowCount():
if role in self.roleNames():
name_role = self.roleNames()[role].decode()
col = self.headers.index(name_role)
return self.rows[row][col]
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole and 0 <= section < len(self.headers):
return self.headers[section]
def roleNames(self):
roles = {}
for i, header in enumerate(self.headers):
roles[Qt.UserRole + i + 1] = header.encode()
return roles
@Slot(result="QVariantList")
def roleNameArray(self):
return self.headers
import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 1.4 as QQC1
import TbModel 1.0
QQC1.ApplicationWindow {
id: window
visible: true
width: Screen.width
height: Screen.height
QQC1.TableView
{
id: idtable
width: 600
height: 300
model: TbModel{ }
QQC1.TableViewColumn {
role: "sci"
title: "sci"
}
QQC1.TableViewColumn {
role: "year"
title: "year"
}
QQC1.TableViewColumn {
role: "cont"
title: "cont"
}
}
}
输出:
推荐阅读
- typescript - 如何使用一种类型键入具有相同结构的函数签名和数组
- javascript - 如何在 webpack 中创建具有给定名称的输出文件名?
- azure-devops - 如何使用 Azure DevOps REST API 将条目添加到工作项的历史记录?
- r - 合并前检查对象是否存在
- mysql - 从一个表求和到另一个表的 SQL 查询
- python - 编译错误 OpenCV 4.2.0 Ubuntu 18.04:目标 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' 失败
- java - 画一个棋盘,每个棋子有 0.4 的机会被填满
- c# - C# DocuSign API:在 Docusign 上提取服务器模板而不是本地模板文档
- javascript - 嵌套组件可以改变 React 上下文吗?
- jquery - 为什么溢出不保持在底部?