Button Delegate Problem in TableView

  • Hi,

    I have subclass a class from QItemDelegate to have a button in tableView.

    I've done this But there is problem with mouse hover in this button.
    Other buttons as you know get focused and highlighted when mouse is hovered
    But in this case nothing happens when mouse enters the button. How can i do this??
    I think somethings have to added into editorEvent method, but in this method i haven't
    access to those buttons which have painted in paint method...
    Any suggestion?

    Paint Method:
    void TButtonDelegate::paint(QPainter *painter,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const

    QStyleOptionButton button;
    // getting the rect of the cell
    QRect r = option.rect;
    int x,y,w,h;
    // The x coordinate of the cell
    x = r.left() + r.width() - 30;
    y = r.top();
    w = 30;
    h = r.height();
    button.rect = QRect(x,y,w,h);
    button.text = "<=";
    button.state = QStyle::State_Enabled;
    painter->drawText(QRect(r.left() + 5,r.top(),r.width() - w,r.height()),
                      (Qt::AlignVCenter | Qt::AlignLeft),


    EditorEvent Method
    bool TButtonDelegate::editorEvent(QEvent *event,
    QAbstractItemModel *model,
    const QStyleOptionViewItem &option,
    const QModelIndex &index)
    if( event->type() == QEvent::MouseButtonRelease )
    QMouseEvent * e = (QMouseEvent *)event;

        int clickX = e->x();
        int clickY = e->y();
        QRect r = option.rect;//getting the rect of the cell
        int x,y,w,h;
        x = r.left() + r.width() - 30;//the X coordinate
        y = r.top();//the Y coordinate
        w = 30;//button width
        h = 30;//button height
        if( clickX > x && clickX < x + w )
            if( clickY > y && clickY < y + h )
                emit cellButtonClicked(index);
                return true;
    return QItemDelegate::editorEvent(event,model,option,index);


  • I think i have to change state of button , does anyone knows how can i do that?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.