Results 1 to 4 of 4

Thread: Button Delegate Problem in TableView

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Button Delegate Problem in TableView

    Hi,

    I have subclass from QItemDelegate to have a button in tableView Just like this:

    qbn7l.png

    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:
    Qt Code:
    1. void TButtonDelegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. Q_UNUSED(index);
    6.  
    7.  
    8. // getting the rect of the cell
    9. QRect r = option.rect;
    10.  
    11. int x,y,w,h;
    12.  
    13. // The x coordinate of the cell
    14. x = r.left() + r.width() - 30;
    15.  
    16. y = r.top();
    17.  
    18. w = 30;
    19.  
    20. h = r.height();
    21.  
    22. button.rect = QRect(x,y,w,h);
    23.  
    24. button.text = "<=";
    25.  
    26. button.state = QStyle::State_Enabled;
    27.  
    28. QApplication::style()->drawControl(QStyle::CE_PushButton,
    29. &button,painter);
    30.  
    31. painter->drawText(QRect(r.left() + 5,r.top(),r.width() - w,r.height()),
    32. (Qt::AlignVCenter | Qt::AlignLeft),
    33. index.data(Qt::DisplayRole).toString());
    34. }
    To copy to clipboard, switch view to plain text mode 

    Editor Event:
    Qt Code:
    1. bool TButtonDelegate::editorEvent(QEvent *event,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index)
    4. {
    5. if( event->type() == QEvent::MouseButtonRelease )
    6. {
    7. QMouseEvent * e = (QMouseEvent *)event;
    8.  
    9. int clickX = e->x();
    10.  
    11. int clickY = e->y();
    12.  
    13. QRect r = option.rect;//getting the rect of the cell
    14.  
    15. int x,y,w,h;
    16.  
    17. x = r.left() + r.width() - 30;//the X coordinate
    18.  
    19. y = r.top();//the Y coordinate
    20.  
    21. w = 30;//button width
    22.  
    23. h = 30;//button height
    24.  
    25. if( clickX > x && clickX < x + w )
    26. if( clickY > y && clickY < y + h )
    27. {
    28. emit cellButtonClicked(index);
    29. return true;
    30. }
    31. }
    32.  
    33. return QItemDelegate::editorEvent(event,model,option,index);
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by alizadeh91; 17th March 2013 at 10:34.

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Button Delegate Problem in TableView

    I think i have to change state of button, but how can i do that when mouse is over the button? !


    Added after 1 30 minutes:


    I've Solved. But in this way can the button get stylesheet?
    Qt Code:
    1. void TButtonDelegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. Q_UNUSED(index);
    6.  
    7.  
    8. // getting the rect of the cell
    9. QRect r = option.rect;
    10.  
    11. int x,y,w,h;
    12.  
    13. // The x coordinate of the cell
    14. x = r.left() + r.width() - 30;
    15.  
    16. y = r.top();
    17.  
    18. w = 30;
    19.  
    20. h = r.height();
    21.  
    22. button.rect = QRect(x,y,w,h);
    23.  
    24. button.text = "<=";
    25.  
    26. button.state = option.state;
    27.  
    28. QApplication::style()->drawControl(QStyle::CE_PushButton,
    29. &button,painter);
    30.  
    31. painter->drawText(QRect(r.left() + 5,r.top(),r.width() - w,r.height()),
    32. (Qt::AlignVCenter | Qt::AlignLeft),
    33. index.data(Qt::DisplayRole).toString());
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by alizadeh91; 17th March 2013 at 12:59.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Button Delegate Problem in TableView

    There is no button here. You only paint something that looks like a button. Therefore you need to do everything manually. As for the stylesheet, probably the only way to make it work is to set the stylesheet on the application object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Button Delegate Problem in TableView

    Thanks wysota Yeah I know that I just wanted to know if is there any way.

Similar Threads

  1. multiselection in a tableview with CTRL Button
    By k.qasempour in forum Newbie
    Replies: 2
    Last Post: 17th July 2012, 11:39
  2. Replies: 4
    Last Post: 22nd September 2010, 23:34
  3. problem with getting info from tableView
    By tinysoft in forum Newbie
    Replies: 0
    Last Post: 19th August 2010, 13:33
  4. tableView Problem
    By hrcariaga in forum Newbie
    Replies: 5
    Last Post: 7th February 2008, 09:29
  5. TableView Delegate questions
    By No-Nonsense in forum Qt Programming
    Replies: 3
    Last Post: 11th December 2006, 10:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.