Results 1 to 8 of 8

Thread: All QStyleOptionButton in listview sunken when clicking one of them

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default All QStyleOptionButton in listview sunken when clicking one of them

    HI, I have the following strange behaviour: I created a QListView whose elements are delegates. The delegate contains a button implemented this way:

    Qt Code:
    1. void ItemDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    2. {
    3. ...
    4. button.rect = buttonRect;
    5. button.features |= QStyleOptionButton::Flat;
    6. button.state = _state | QStyle::State_Enabled;
    7. QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
    8. ...
    9. }
    10.  
    11. bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    12. {
    13. if( event->type() == QEvent::MouseButtonPress ||
    14. event->type() == QEvent::MouseButtonRelease ) {
    15. } else {
    16. _state = QStyle::State_Raised;
    17. return true;
    18. }
    19.  
    20. ...
    21. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
    22. if( !buttonRect.contains( mouseEvent->pos()) ) {
    23. _state = QStyle::State_Raised;
    24. return true;
    25. }
    26.  
    27. if( event->type() == QEvent::MouseButtonPress) {
    28. _state = QStyle::State_Sunken;
    29. } else if( event->type() == QEvent::MouseButtonRelease) {
    30. _state = QStyle::State_Raised;
    31. // call slot
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    It works fine except that the first time (after starting the application) I click any button, all buttons show the State_Sunken; all others clicks, after the first one, are normals: only the clicked button show the sunken/raised effect.

    Do you have any explaination for this?

    Very thanks.
    Giuseppe CalÃ

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    Where is buttonrect defined/coming from?

    The items in the view are all drawn by the same, single delegate (i.e. the list view items are not delegates). If buttonrect is kept state then then painting will be confused.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    Here the code (the same for paint() and editorEvent()):

    Qt Code:
    1. QRect buttonRect;
    2. buttonRect.setX(option.rect.width() - buttonSize - margin);
    3. buttonRect.setY(option.rect.y() + (option.rect.height()/2 - buttonSize/2));
    4. buttonRect.setWidth(buttonSize);
    5. buttonRect.setHeight(buttonSize);
    To copy to clipboard, switch view to plain text mode 

    I omitted it in first post because, I thought, not significant.
    Giuseppe CalÃ

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    And _state? If this is a member variable then that single _state is used when rendering any of the items the view needs to paint.

  5. #5
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    _state is effectively a member variable:

    Qt Code:
    1. QStyle::State _state;
    To copy to clipboard, switch view to plain text mode 

    but if this is the problem, why the behaviour happens only on very first click and not in next?
    Giuseppe CalÃ

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    Are you initialising _state to QStyle::State_Raised in the constructor ?

  7. #7
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    This is my constructor:

    Qt Code:
    1. ItemDelegate::ItemDelegate(QObject *parent) :
    2. {
    3. _state = QStyle::State_Enabled;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your patience
    Giuseppe CalÃ

  8. #8
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: All QStyleOptionButton in listview sunken when clicking one of them

    I solved changing approach; I'm not using anymore _state member but int _currRow to track clicked button; here it is a sample code:

    Qt Code:
    1. ItemDelegate::ItemDelegate(QObject *parent) :
    2. {
    3. _currRow = -1;
    4. }
    5.  
    6. void ItemDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    7. {
    8. if (!index.isValid())
    9. return;
    10.  
    11. QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
    12.  
    13. ...
    14.  
    15. button.rect = buttonRect;
    16. button.features |= QStyleOptionButton::Flat;
    17.  
    18. if(_currRow == index.row())
    19. button.state = QStyle::State_Sunken | QStyle::State_Enabled;
    20. else
    21. button.state = QStyle::State_Raised | QStyle::State_Enabled;
    22.  
    23. QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
    24. }
    25.  
    26. bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    27. {
    28. Q_UNUSED(model)
    29.  
    30. if( event->type() != QEvent::MouseButtonPress &&
    31. event->type() != QEvent::MouseButtonRelease ) {
    32. return true;
    33. }
    34.  
    35. if( event->type() == QEvent::MouseButtonPress)
    36. _currRow = index.row();
    37. else
    38. _currRow = -1;
    39.  
    40. ...
    41.  
    42. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
    43. if( !buttonRect.contains( mouseEvent->pos()) ) {
    44. return true;
    45. }
    46.  
    47. if( event->type() == QEvent::MouseButtonRelease) {
    48. // call slot
    49. }
    50.  
    51. return true;
    52. }
    To copy to clipboard, switch view to plain text mode 

    Thanks ChrisW67 for your time.
    Giuseppe CalÃ

Similar Threads

  1. Adding stylesheet and icon to QStyleOptionButton
    By riarioriu3 in forum Newbie
    Replies: 0
    Last Post: 8th August 2012, 14:12
  2. Clicking on a plot
    By Ronayn in forum Qwt
    Replies: 1
    Last Post: 29th April 2011, 13:59
  3. Clicking on QwtPlot
    By orignihn in forum Qwt
    Replies: 1
    Last Post: 14th April 2011, 16:37
  4. Remove shadow of child widgets? Like a sunken frame...
    By new_voodoo in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2010, 23:31
  5. Clicking Whitespace in a table
    By shooogun in forum Qt Programming
    Replies: 5
    Last Post: 27th March 2008, 06:29

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.