Qt 5.10 Book Examples
CustomWidget.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // CustomWidget.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 "CustomWidget.h"
13 
14 // ----------------------------------------------------------------------
15 CustomWidget::CustomWidget(QWidget* pwgt/*= 0*/) : QFrame(pwgt)
16  , m_nProgress(0)
17 {
18  setLineWidth(3);
19  setFrameStyle(Box | Sunken);
20 
21  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
22 }
23 
24 // ----------------------------------------------------------------------
25 /*virtual*/ void CustomWidget::paintEvent(QPaintEvent*)
26 {
27  QPainter painter(this);
28  QLinearGradient gradient(0, 0, width(), height());
29  float f = m_nProgress / 100.0f;
30 
31  gradient.setColorAt(0, Qt::black);
32  gradient.setColorAt(f, Qt::green);
33 
34  painter.fillRect(rect(), Qt::black);
35  painter.setBrush(gradient);
36  painter.drawRect(0, 0, (int)(width() * f), height());
37 
38  painter.setPen(QPen(Qt::green));
39  QString str = QString().setNum(m_nProgress) + "%";
40  painter.drawText(rect(), Qt::AlignCenter, str);
41 
42  drawFrame(&painter);
43 }
44 
45 // ----------------------------------------------------------------------
47 {
48  m_nProgress = n > 100 ? 100 : n < 0 ? 0 : n;
49  repaint();
51 }
52 
53 // ----------------------------------------------------------------------
54 /*virtual*/ QSize CustomWidget::sizeHint() const
55 {
56  return QSize(200, 30);
57 }
58 
void slotSetProgress(int n)
void progressChanged(int)
virtual void paintEvent(QPaintEvent *)
virtual QSize sizeHint() const
CustomWidget(QWidget *pwgt=0)