Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Button Delegate Problem in TableView
Forum Updated to NodeBB v4.3 + New Features

Button Delegate Problem in TableView

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alizadeh91
    wrote on last edited by
    #1

    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
    {
    Q_UNUSED(index);

    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;
    
    QApplication::style()->drawControl(QStyle::CE_PushButton,
                                       &button,painter);
    
    painter->drawText(QRect(r.left() + 5,r.top(),r.width() - w,r.height()),
                      (Qt::AlignVCenter | Qt::AlignLeft),
                      index.data(Qt::DisplayRole).toString());
    

    }
    @

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

    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alizadeh91
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved