Results 1 to 2 of 2

Thread: Custom QPushButton (using StyleSheets) will not draw selected

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Custom QPushButton (using StyleSheets) will not draw selected

    I have created a custom QPushButton so if a button has both an icon and text I can place draw the text below the button image. My button image has 3D borders so am using stylesheets to provide non-scaling borders.

    The the button looks correct in all the states but selected or pressed, so I know it understands the state changes and is getting the images from the style sheet. My first guess was that the style sheet image was incorrect.

    When I comment out the paintEvent() function all the states draw properly even though the text is drawn on the button. So I know the button is getting the mouse event, the style sheet images are correct, the button is getting the press event, and the style sheet can correctly be used for all the state changes. What in this paintEvent() could possibly prevent the button from drawing itself selected if it will draw itself in the hover state?

    Here is the style sheet code:
    Qt Code:
    1. MyButton{
    2. outline: none; /* remove focus rectangle */
    3. text-overflow: ellipsis;
    4. border-image: url(:/images/Resources/Images/Active.png) 9;
    5. border: 9px transparent;
    6. }
    7. MyButton:selected {
    8. border-image: url(:/images/Resources/Images/Selected.png) 9;
    9. }
    10. MyButton:focus {
    11. border-image: url(:/images/Resources/Images/Focus.png) 9;
    12. }
    13. MyButton:hover {
    14. border-image: url(:/images/Resources/Images/Focus.png) 9;
    15. }
    16. MyButton:pressed {
    17. border-image: url(:/images/Resources/Images/Selected.png) 9;
    18. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void MyPushButton::paintEvent ( QPaintEvent * pEvent )
    2. {
    3. QPainter p(this);
    4. QRect iconRect;
    5. QRect textRect;
    6. QRect btnRect = this->rect();
    7.  
    8. opt.init(this);
    9. if (!this->icon().isNull() && !this->text().isEmpty()) {
    10. //decrease the height of the drawing area so it doesn't cover the text
    11. opt.rect.adjust(0,0,0,-(fontMetrics().height() * 2));
    12. }
    13.  
    14. //draw the backround
    15. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    16.  
    17. /*
    18. Tried adding this and removing the "outline: none;" from the style sheet with no affect
    19. I added this with the conditional to verify that it was getting focus…
    20.   //draw the focus/selection rect
    21.   if (opt.state & QStyle::State_HasFocus) {
    22.   QStyleOptionFocusRect focusOpt;
    23.   focusOpt.QStyleOption::operator=(opt);
    24.   //focusOpt.state | QStyle::State_FocusAtBorder; // tried with and without – no change
    25.   style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOpt, &p, this);
    26.   }
    27. */
    28. if (!this->icon().isNull())
    29. {
    30. iconRect.setRect(10,10, opt.rect.width()-30, opt.rect.height()-20);
    31.  
    32. //Draw the icon
    33. style()->drawItemPixmap(&p, iconRect, Qt::AlignLeft | Qt::AlignVCenter,
    34. this->icon().pixmap(iconRect.size()));
    35.  
    36. if (!this->text().isEmpty()) {
    37. opt.rect.adjust(0,0,0,fontMetrics().height() * 2);
    38. // This button has an icon and text. The text should be drawn below
    39. textRect.setRect(btnRect.x(),
    40. 45,
    41. btnRect.width(),
    42. fontMetrics().height() * 2);
    43. style()->drawItemText(&p, textRect, Qt::AlignCenter, (this->palette()),
    44. true, this->text(), QPalette::ButtonText );
    45. }
    46.  
    47. } else {
    48. if (!this->text().isEmpty()) {
    49. // this button has text but no icon
    50. // Draw the text centered on the button
    51. style()->drawItemText(&p, this->rect(), Qt::AlignCenter, (this->palette()),
    52. true, this->text());
    53. }
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QPushButton (using StyleSheets) will not draw selected

    I can't seem to edit the original version so here is what I have discovered...
    The problem is with call to style()->drawPrimitive(). I have read that style sheets and QStyle don't pay well together. I didn't think that was my problem because every other state (over, focus, disabled, active) draws correctly. Can someone tell me that is in fact my problem or is there some other call I make to draw the background of a button within a limited area (not the entire widget) while still utilizing the scalability of border-image found in stylesheets?

Similar Threads

  1. Stylesheets & QPushButton menus
    By zuck in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2011, 16:40
  2. Replies: 4
    Last Post: 16th September 2011, 03:10
  3. Custom painting and stylesheets
    By arturo182 in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2009, 12:14
  4. QPushButton, stylesheets, text alignment
    By SiLiZiUMM in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 20:53
  5. Replies: 1
    Last Post: 5th March 2007, 21:50

Tags for this Thread

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.