我正在尝试在表格视图中每行嵌入一个按钮。我的按钮绘制正确,但对任何点击都没有反应。我应该为此列设置标志吗?到目前为止,我有类似的东西:
if index.column() == 14:
flags |= QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable | Qt.ItemIsEnabled
return flags
这是代表:
class AButton(QtGui.QStyledItemDelegate):
mouse_isPressed = False
def __init__(self, parent = None):
QtGui.QStyledItemDelegate.__init__(self, parent)
def boundingRect(self):
return QtCore.QRectF(0, 0, 40, 40)
def paint(self, painter, option, widget = 0):
opt = QtGui.QStyleOptionButton()
opt.state = ((QtGui.QStyle.State_Sunken if self.mouse_isPressed else QtGui.QStyle.State_Raised) | QtGui.QStyle.State_Enabled)
opt.text = self.text()
opt.icon = self.icon()
opt.rect = option.rect
opt.palette = option.palette
QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_PushButton, opt, painter)
def text(self):
return QtCore.QString("hi")
def icon(self):
return QtGui.QIcon()
def mousePressEvent(self, event):
self.mouse_isPressed = True
print "HELLO"
self.update()
def mouseReleaseEvent(self, event):
self.mouse_isPressed = False
self.update()
有没有我可以看的例子?
在此先感谢,克里斯