我正在写一个Twitter客户.我实现了TweetItem和TweetModel.问题在于,TweetItem中有一个称为原始的角色.我希望它指向原始推文.
更新:我的代码中有一些错字.现在,我修复了它们.
import sys
from PyQt4 import QtCore, QtGui, QtDeclarative
class TweetModel(QtCore.QAbstractListModel):
def __init__(self, prototype, parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self.setRoleNames(prototype.roleNames())
self.tweets = []
def appendRow(self, item):
self.tweets.append(item)
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.tweets)
def data(self, index, role):
return self.tweets[index.row()].data(role)
class TweetItem(QtCore.QAbstractItemModel):
def __init__(self, id=None, original=None, parent=None):
QtCore.QAbstractItemModel.__init__(self, parent)
self.idRole = QtCore.Qt.UserRole + 1
# More Roles
self.originalRole = QtCore.Qt.UserRole + 6
self.id = id
self.original = original
def roleNames(self):
names = {}
names[self.idRole] = "id"
names[self.originalRole] = "original"
return names
def data(self, role):
if role == self.idRole:
return self.id
elif role == self.originalRole:
# self.original == <__main__.TweetItem object at 0x7fb703d95d40>
return self.original
else:
return None
if __name__ == "__main__":
model = TweetModel(TweetItem())
item = TweetItem("0001", None, model)
model.appendRow(TweetItem("0002", item, model))
App = QtGui.QApplication(sys.argv)
view = QtDeclarative.QDeclarativeView()
view.rootContext().setContextProperty("mymodel", model)
view.setSource(QtCore.QUrl.fromLocalFile("main.qml"))
view.show()
App.exec_()
但是我不能在QML中使用它.我得到一个未定义的值.
import QtQuick 1.0
Rectangle {
width: 360
height: 360
ListView {
anchors.fill: parent
model: mymodel
// original.id == undefined
delegate: Component { Text { text: id + " " + original.id } }
}
}
那么,有可能返回一个角色中的对象并使用它吗?
解决方法:
正如dant3所建议的,将QObject与属性一起使用.
这是一个示例如何做:
import sys
from PyQt4 import QtCore, QtGui, QtDeclarative
from PyQt4.QtCore import pyqtProperty, pyqtSignal, QObject
class TweetModel(QtCore.QAbstractListModel):
def __init__(self, prototype, parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self.setRoleNames(prototype.roles)
self.tweets = []
def appendRow(self, item):
self.tweets.append(item)
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.tweets)
def data(self, index, role):
return self.tweets[index.row()].data(role)
class TweetItem(QObject):
roles = {
QtCore.Qt.UserRole + 1: 'id',
QtCore.Qt.UserRole + 6: 'original',
}
id_changed = pyqtSignal()
def __init__(self, id=None, original=None, parent=None):
QObject.__init__(self, parent=parent)
self._data = {'original': original}
self.id = id
def data(self, key):
return self._data[self.roles[key]]
@pyqtProperty(str, notify=id_changed)
def id(self):
return self._data['id']
@id.setter
def id(self, value):
if self._data.get('id') != value:
self._data['id'] = value
self.id_changed.emit()
if __name__ == "__main__":
model = TweetModel(TweetItem)
item = TweetItem("0001", None, model)
model.appendRow(TweetItem("0002", item, model))
App = QtGui.QApplication(sys.argv)
view = QtDeclarative.QDeclarativeView()
view.rootContext().setContextProperty("mymodel", model)
view.setSource(QtCore.QUrl.fromLocalFile("main.qml"))
view.show()
App.exec_()
QML文件保持不变.
我没有将original设为属性,因为您可以将其作为模型数据来获取,但是可以按照与id相同的方式进行设置.