Qt 5.10 Book Examples
CustomStyle.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // CustomStyle.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtGui>
12 #include "CustomStyle.h"
13 
14 // ----------------------------------------------------------------------
15 void CustomStyle::polish(QWidget* pwgt)
16 {
17  if (qobject_cast<QPushButton*>(pwgt)) {
18  pwgt->setAttribute(Qt::WA_Hover, true);
19  }
20 }
21 
22 // ----------------------------------------------------------------------
23 void CustomStyle::unpolish(QWidget* pwgt)
24 {
25  if (qobject_cast<QPushButton*>(pwgt)) {
26  pwgt->setAttribute(Qt::WA_Hover, false);
27  }
28 }
29 
30 // ----------------------------------------------------------------------
31 void CustomStyle::drawPrimitive( PrimitiveElement elem,
32  const QStyleOption* popt,
33  QPainter* ppainter,
34  const QWidget* pwgt
35  ) const
36 {
37  switch (elem) {
38  case PE_PanelButtonCommand:
39  {
40  const QStyleOptionButton* pOptionButton =
41  qstyleoption_cast<const QStyleOptionButton*>(popt);
42  if (pOptionButton) {
43  bool bDown = (pOptionButton->state & State_Sunken)
44  || (pOptionButton->state & State_On);
45 
46  QPixmap pix = bDown ? QPixmap(":/images/btnprs.bmp")
47  : QPixmap(":/images/btn.bmp");
48 
49  ppainter->drawPixmap(pOptionButton->rect, pix);
50 
51  bool bHover = (pOptionButton->state & State_Enabled)
52  && (pOptionButton->state & State_MouseOver);
53  if (bHover) {
54  ppainter->fillRect(pOptionButton->rect,
55  QColor(25, 97, 45, 83)
56  );
57  }
58  }
59  break;
60  }
61 
62  default:
63  QCommonStyle::drawPrimitive(elem, popt, ppainter, pwgt);
64  break;
65  }
66  return;
67 }
virtual void unpolish(QWidget *pwgt)
Definition: CustomStyle.cpp:23
virtual void drawPrimitive(PrimitiveElement elem, const QStyleOption *popt, QPainter *ppainter, const QWidget *pwgt=0) const
Definition: CustomStyle.cpp:31
virtual void polish(QWidget *pwgt)
Definition: CustomStyle.cpp:15