问题描述
我尝试使用 PyQt5.6 (Python 3.4) 和 QML 设置一个简单的树视图示例.我刚刚找到了一些 C++ 示例,但与 PyQt 无关.我拿起了 PyQt 源代码附带的 simpletreemodel 示例并对其进行了修改(https://github.com/baoboa/pyqt5/tree/master/examples/itemviews/simpletreemodel).
I try to setup a simple treeview example with PyQt5.6 (Python 3.4) and QML. I just found a few C++ examples, but nothing related to PyQt. I picked up the simpletreemodel example comming with PyQt source and modified it (https://github.com/baoboa/pyqt5/tree/master/examples/itemviews/simpletreemodel).
很可能模型有问题.我收到两条错误消息:
Most likely something is wrong with the model. I got two error messages:
simpletreemodel.qml:13:5:QML TreeView:检测到属性model"的绑定循环
...qt/5.6/gcc_64/qml/QtQuick/Controls/TreeView.qml:94:16: 无法将 [undefined] 分配给 QAbstractItemModel*
知道发生了什么和/或错误吗?qml 树视图刚刚在 Qt 5.5 中引入,也许它不能在 PyQt 中完全工作?!没有找到任何相关信息.
Any idea whats going on and/or wrong? The qml treeview was just introduced in Qt 5.5 and maybe its not fully working in PyQt?! Didn't found any information about that.
这是我的代码:
simpletreemodel.qml
simpletreemodel.qml
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQml.Models 2.2
Rectangle {
width: 480
height: 640
TreeView {
id: treeView
anchors.fill: parent
anchors.margins: 6
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
model: model
TableViewColumn {
title: "Title"
role: "TitleRole"
resizable: true
}
TableViewColumn {
title: "Summary"
role: "SummaryRole"
resizable: true
}
}
}
simpletreemodel.py
simpletreemodel.py
#!/usr/bin/env python
from PyQt5.QtCore import (
QAbstractItemModel, QFile,
QIODevice, QModelIndex, Qt,
QUrl
)
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
import simpletreemodel_rc
class TreeItem(object):
def __init__(self, data, parent=None):
self.parentItem = parent
self.itemData = data
self.childItems = []
def appendChild(self, item):
self.childItems.append(item)
def child(self, row):
return self.childItems[row]
def childCount(self):
return len(self.childItems)
def columnCount(self):
return len(self.itemData)
def data(self, column):
try:
return self.itemData[column]
except IndexError:
return None
def parent(self):
return self.parentItem
def row(self):
if self.parentItem:
return self.parentItem.childItems.index(self)
return 0
class TreeModel(QAbstractItemModel):
def __init__(self, data, parent=None):
super(TreeModel, self).__init__(parent)
self.rootItem = TreeItem(("Title", "Summary"))
self.setupModelData(data.split('\n'), self.rootItem)
def roleNames(self):
roles = {
Qt.UserRole + 1: b"TitleRole",
Qt.UserRole + 2: b"SummaryRole"
}
return roles
def columnCount(self, parent):
if parent.isValid():
return parent.internalPointer().columnCount()
else:
return self.rootItem.columnCount()
def data(self, index, role):
if not index.isValid():
return None
if role != Qt.DisplayRole:
return None
item = index.internalPointer()
return item.data(index.column())
def flags(self, index):
if not index.isValid():
return Qt.NoItemFlags
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
def headerData(self, section, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.rootItem.data(section)
return None
def index(self, row, column, parent):
if not self.hasIndex(row, column, parent):
return QModelIndex()
if not parent.isValid():
parentItem = self.rootItem
else:
parentItem = parent.internalPointer()
childItem = parentItem.child(row)
if childItem:
return self.createIndex(row, column, childItem)
else:
return QModelIndex()
def parent(self, index):
if not index.isValid():
return QModelIndex()
childItem = index.internalPointer()
parentItem = childItem.parent()
if parentItem == self.rootItem:
return QModelIndex()
return self.createIndex(parentItem.row(), 0, parentItem)
def rowCount(self, parent):
if parent.column() > 0:
return 0
if not parent.isValid():
parentItem = self.rootItem
else:
parentItem = parent.internalPointer()
return parentItem.childCount()
def setupModelData(self, lines, parent):
parents = [parent]
indentations = [0]
number = 0
while number < len(lines):
position = 0
while position < len(lines[number]):
if lines[number][position] != ' ':
break
position += 1
lineData = lines[number][position:].trimmed()
if lineData:
# Read the column data from the rest of the line.
columnData = [s for s in lineData.split('\t') if s]
if position > indentations[-1]:
# The last child of the current parent is now the new
# parent unless the current parent has no children.
if parents[-1].childCount() > 0:
parents.append(parents[-1].child(parents[-1].childCount() - 1))
indentations.append(position)
else:
while position < indentations[-1] and len(parents) > 0:
parents.pop()
indentations.pop()
# Append a new item to the current parent's list of children.
parents[-1].appendChild(TreeItem(columnData, parents[-1]))
number += 1
if __name__ == '__main__':
import sys
app = QGuiApplication(sys.argv)
view = QQuickView()
f = QFile(':/default.txt')
f.open(QIODevice.ReadOnly)
model = TreeModel(f.readAll())
f.close()
root_context = view.rootContext().setContextProperty('model', model)
view.setSource(QUrl.fromLocalFile('simpletreemodel.qml'))
view.show()
sys.exit(app.exec_())
推荐答案
root_context = view.rootContext().setContextProperty('model', model)
不要使用model作为模型的名称.
Don't use model as the name of model.
这篇关于PyQt5 QML 树状视图示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!