【问题标题】:button delegate in a tableview表格视图中的按钮委托
【发布时间】:2012-11-07 22:54:37
【问题描述】:

我正在尝试在表格视图中每行嵌入一个按钮。我的按钮绘制正确,但对任何点击都没有反应。我应该为此列设置标志吗?到目前为止,我有类似的东西:

    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()

有没有我可以看的例子?

提前致谢, 危机

【问题讨论】:

    标签: pyqt pyqt4


    【解决方案1】:

    Qt 委托不像 QWidget 那样提供特定的事件处理程序(如 mousePressEvent、mouseReleaseEvent、...)。 如果您想对用户操作做出反应,您应该重新实现 editorEvent 方法。 顺便说一句,QStyledItemDelegate中没有定义这样的'update'方法

    【讨论】:

    • 您好,感谢您的回复,我现在知道了。我正在使用编辑器方法让我的按钮做出反应。只有一件事困扰着我,不确定是限制还是我可以解决的问题。为了能够按下按钮,我需要通过双击“激活”包含单元格。一旦单元格处于活动状态,我就可以按下按钮。这可能会让您的普通用户感到困惑。有什么想法吗?
    猜你喜欢
    • 2016-06-19
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多