带有代理的可展开表格的Pyside示例
有没有人知道一个例子,使用Pyside创建一个可以动态调整大小的表格,并且在某一列中使用下拉框作为代理?我尝试修改这个例子,但每次都只能在选中单元格时才显示下拉框。
这是我开始的例子:
import sys
from PySide import QtCore, QtGui
class TableModel(QtCore.QAbstractTableModel):
"""
A simple 5x4 table model to demonstrate the delegates
"""
def rowCount(self, parent=QtCore.QModelIndex()): return 5
def columnCount(self, parent=QtCore.QModelIndex()): return 4
def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid(): return None
if not role==QtCore.Qt.DisplayRole: return None
return "{0:02d}".format(index.row())
def setData(self, index, value, role=QtCore.Qt.DisplayRole):
print "setData", index.row(), index.column(), value
def flags(self, index):
if (index.column() == 0):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled
else:
return QtCore.Qt.ItemIsEnabled
class ComboDelegate(QtGui.QItemDelegate):
"""
A delegate that places a fully functioning QComboBox in every
cell of the column to which it's applied
"""
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
combo = QtGui.QComboBox(parent)
li = []
li.append("Zero")
li.append("One")
li.append("Two")
li.append("Three")
li.append("Four")
li.append("Five")
combo.addItems(li)
self.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("currentIndexChanged()"))
return combo
def setEditorData(self, editor, index):
editor.blockSignals(True)
editor.setCurrentIndex(int(index.model().data(index)))
editor.blockSignals(False)
def setModelData(self, editor, model, index):
model.setData(index, editor.currentIndex())
#@QtCore.pyqtSlot()
def currentIndexChanged(self):
self.commitData.emit(self.sender())
class TableView(QtGui.QTableView):
"""
A simple table to demonstrate the QComboBox delegate.
"""
def __init__(self, *args, **kwargs):
QtGui.QTableView.__init__(self, *args, **kwargs)
# Set the delegate for column 0 of our table
# self.setItemDelegateForColumn(0, ButtonDelegate(self))
self.setItemDelegateForColumn(0, ComboDelegate(self))
if __name__=="__main__":
from sys import argv, exit
class Widget(QtGui.QWidget):
"""
A simple test widget to contain and own the model and table.
"""
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
l=QtGui.QVBoxLayout(self)
self._tm=TableModel(self)
self._tv=TableView(self)
self._tv.setModel(self._tm)
for row in range(0, self._tm.rowCount()):
self._tv.openPersistentEditor(self._tm.index(row, 0))
l.addWidget(self._tv)
a=QtGui.QApplication(argv)
w=Widget()
w.show()
w.raise_()
exit(a.exec_())
1 个回答
1
我把下面这个类修改成了这样
class ComboDelegate(QtGui.QItemDelegate):
"""
A delegate that places a fully functioning QComboBox in every
cell of the column to which it's applied
"""
def __init__(self, parent, options):
QtGui.QItemDelegate.__init__(self, parent)
self.options = options
def createEditor(self, parent, option, index):
combo = QtGui.QComboBox(parent)
combo.addItems(self.options)
self.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("currentIndexChanged()"))
return combo
def setEditorData(self, editor, index):
editor.blockSignals(True)
editor.setCurrentIndex(editor.currentIndex())
editor.blockSignals(False)
def setModelData(self, editor, model, index):
model.setData(index, editor.itemText(editor.currentIndex()))
#@QtCore.pyqtSlot()
def currentIndexChanged(self):
self.commitData.emit(self.sender())
它在 QTableWidget
中运行得非常好
self.Table = QtGui.QTableWidget(parent)
# ... other columns before
# ...
item = QtGui.QTableWidgetItem("DATATYPE")
self.Table.setHorizontalHeaderItem(3, item)
self.Table.horizontalHeaderItem(3).setWhatsThis("DATATYPE")
self.DataType = ComboDelegate(self.MemoryTable, DataTypeOptions)
self.Table.setItemDelegateForColumn(3,self.DataType)
这个例子在表格的第三列的每一行里放了一个下拉框(ComboBox)