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