背景:在QTableView
中找不到组合框的完整工作示例。所以我写这段代码是基于其他几个更为复杂的例子。但是,问题是,这个示例要求您在启用组合框之前双击它,然后您必须再次单击以将其下拉。不太方便使用。如果我使用QTableWidget
执行非模型/视图操作,则第一次单击时组合框将下拉。
问题有人能看看这个问题,告诉我需要做些什么才能让它像QTableWidget
那样做出响应吗?另外,如果有什么我正在做的是不必要的,请注明。例如,是否绝对有必要引用应用程序样式?
import sys
from PyQt4 import QtGui, QtCore
rows = "ABCD"
choices = ['apple', 'orange', 'banana']
class Delegate(QtGui.QItemDelegate):
def __init__(self, owner, items):
super(Delegate, self).__init__(owner)
self.items = items
def createEditor(self, parent, option, index):
self.editor = QtGui.QComboBox(parent)
self.editor.addItems(self.items)
return self.editor
def paint(self, painter, option, index):
value = index.data(QtCore.Qt.DisplayRole).toString()
style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionComboBox()
opt.text = str(value)
opt.rect = option.rect
style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter)
QtGui.QItemDelegate.paint(self, painter, option, index)
def setEditorData(self, editor, index):
value = index.data(QtCore.Qt.DisplayRole).toString()
num = self.items.index(value)
editor.setCurrentIndex(num)
def setModelData(self, editor, model, index):
value = editor.currentText()
model.setData(index, QtCore.Qt.DisplayRole, QtCore.QVariant(value))
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class Model(QtCore.QAbstractTableModel):
def __init__(self):
super(Model, self).__init__()
self.table = [[row, choices[0]] for row in rows]
def rowCount(self, index=QtCore.QModelIndex()):
return len(self.table)
def columnCount(self, index=QtCore.QModelIndex()):
return 2
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return self.table[index.row()][index.column()]
def setData(self, index, role, value):
if role == QtCore.Qt.DisplayRole:
self.table[index.row()][index.column()] = value
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.model = Model()
self.table = QtGui.QTableView()
self.table.setModel(self.model)
self.table.setItemDelegateForColumn(1, Delegate(self, ["apple", "orange", "banana"]))
self.setCentralWidget(self.table)
self.setWindowTitle('Delegate Test')
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Main()
app.exec_()
使用QTableWiget.setCellWidget
如果有人感兴趣,下面是为PyQt5和Python 3修改的相同示例。关键更新包括:
super().__init__()
QtWidgets
;本例不需要QtGui
Model.setData
:输入参数顺序更改为:index, value, role
,并返回True
,而不是None
choices
和表内容现在在Main
中指定;这使Delegate
和Model
更通用如果试图在视图显示编辑器时进行调整,则需要更改qabstractemview中定义的编辑触发器。默认值是doubleClick上的edit,但我认为您需要的是QAbstractItemView.CurrentChanged。通过调用myView.setEditTrigger()来设置
相关问题 更多 >
编程相关推荐