我正在尝试使用QListView
创建QStyledItemDelegate
来显示更多组织方式的数据。
我经历了this site,它全部都是用C ++编写的,而且我对它一无所知,从语法和调用中猜到了帖子,我试着去实现它,但是我没有运气。有人可以帮我解决这个问题吗?
import sys, os
from PyQt4 import QtGui, QtCore
class ListView(QtGui.QListView):
def __init__(self, parent=None):
super(ListView, self).__init__(parent)
self._model = None
self._data = [
[
'Header: King Arthur',
'Project: TBN',
'Asset: arthur',
'Task name: Design doc',
'Start Date: Today',
'End Date: Next Monday'
]
]
self.set_model()
item_delegate = ItemDelegate()
self.setItemDelegate(item_delegate)
self.openPersistentEditor(self._model.createIndex(0, 0))
def set_model(self):
self._model = ListModel(self._data, parent=self)
self.setModel(self._model)
class ListModel(QtCore.QAbstractItemModel):
def __init__(self, data=[], parent=None):
super(ListModel, self).__init__(parent)
self._data = data
def rowCount(self, *arg):
return 1
def columnCount(self, *arg):
return len(self._data)
def data(self, index, role):
row = index.row()
column = index.column()
if role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(' | '.join(self._data[column]))
return QtCore.QVariant()
def index(self, row, column, parent):
return self.createIndex(row, column)
def parent(self, index):
item = index.internalPointer()
if item:
return item.getParent()
else:
item = self.createIndex(index.row(), index.column()).internalPointer()
if item:
return item.getParent()
return QtCore.QModelIndex()
class ItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
item_data = str(index.data().toString())
editor = Widget(item_data.split('|'), parent=parent)
return editor
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class Widget(QtGui.QWidget):
def __init__(self, widget_data=[], parent=None):
super(Widget, self).__init__(parent)
vbox = QtGui.QVBoxLayout(self)
key_font = QtGui.QFont()
key_font.setWeight(QtGui.QFont.Bold)
val_font = QtGui.QFont()
val_font.setWeight(QtGui.QFont.Normal)
for each_data in widget_data:
hbox = QtGui.QHBoxLayout()
key, value = each_data.split(':')
key_text = QtGui.QLabel(self)
val_text = QtGui.QLabel(self)
key_text.setToolTip('Key: %s' % key)
val_text.setToolTip('Value: %s' % value)
key_text.setText(key)
val_text.setText(value)
key_text.setFont(key_font)
val_text.setFont(val_font)
hbox.addWidget(key_text)
hbox.addWidget(val_text)
# vbox.addLayout(hbox)
if __name__ == '__main__':
qapp = QtGui.QApplication([])
app = ListView()
app.show()
sys.exit(qapp.exec_())
答案 0 :(得分:0)
除非您有令人信服的理由使用视图/模型模式,否则在大多数情况下,使用窗口小部件/项目模式会更容易 - QListWidget
和QListWidgetItem
。
QItemDelegate
和QStyledItemDelegate
设计为子类。然后,您可以定义负责处理事件,调整大小和绘制视图/小部件的方法。
class MyWidget(QtGui.QWidget):
def __init__(self, parent):
super(MyWidget, self).__init__(parent)
self.listwidget = QtGui.QListWidget(self)
self.delegate = MyDelegate(self, self.listwidget)
self.listwidget.setItemDelegate(self.delegate)
datas = [
{'Header': 'Blah', 'Project': 'TBN'},
{'Header': 'Other', 'Project': 'Something'},
]
for data in datas:
MyItem(self.listwidget, data)
class MyItem(QtGui.QListWidgetItem):
def __init__(self, parent, data):
super(MyItem, self).__init__(parent)
self._data = data
class MyDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent, listwidget):
super(MyDelegate, self).__init__(parent)
self.listwidget = listwidget
def sizeHint(self, option, index):
if index:
item = self.listwidget.itemFromIndex(index)
print item._data
# Do fontmetrics stuff from C++ article you linked
# using the text in the data dictionary.
def paint(self, painter, option, index):
# same thing, get the item using the index
# get the data from the item
# paint the data however you want.
答案 1 :(得分:0)
require
import
解决了这个问题