1 #include "pinballwidget.h" 2 #include <QPropertyAnimation> 3 #include <QTimer> 4 #include <QPushButton> 5 #include <QPainter> 6 #include <QTextOption> 7 #include <QDebug> 8 9 class PinBallWidgetPrivate 10 { 11 public: 12 QString number="0"; 13 QString tips; 14 QTimer *timer = Q_NULLPTR; 15 int seconds =0; 16 }; PinBallWidget(QWidget * parent)17PinBallWidget::PinBallWidget(QWidget *parent) : QWidget(parent), 18 m_d(new PinBallWidgetPrivate) 19 { 20 21 } 22 ~PinBallWidget()23PinBallWidget::~PinBallWidget() 24 { 25 delete m_d; 26 } 27 paintEvent(QPaintEvent * e)28void PinBallWidget::paintEvent(QPaintEvent *e) 29 { 30 Q_UNUSED(e) 31 QPainter painter(this); 32 painter.setRenderHints(QPainter::Antialiasing); 33 34 QRectF rect = this->rect(); 35 if(!m_d->tips.isEmpty()) 36 { 37 QTextOption o; 38 o.setAlignment(Qt::AlignCenter); 39 QString text = m_d->tips; 40 QFont font = this->font(); 41 font.setBold(true); 42 font.setPointSize(18); 43 painter.setFont(font); 44 QRectF r = painter.boundingRect(rect, text, o); 45 painter.drawText(r, text); 46 return; 47 } 48 49 QTextOption o; 50 o.setAlignment(Qt::AlignCenter); 51 QString text = QString("Number of dog feeding: %1").arg(m_d->number); 52 QRectF r = painter.boundingRect(rect, text, o); 53 painter.drawText(r, text); 54 55 } 56 setValue(const QString & number)57void PinBallWidget::setValue(const QString &number) 58 { 59 m_d->number = number; 60 this->update(); 61 } 62 setTips(const QString & info)63void PinBallWidget::setTips(const QString &info) 64 { 65 m_d->tips = info; 66 } 67 startDownTime(int maxSeconds)68void PinBallWidget::startDownTime(int maxSeconds) 69 { 70 qDebug()<< "maxSeconds="<< maxSeconds; 71 if(maxSeconds == 0) 72 { 73 m_d->tips.clear(); 74 this->update(); 75 m_d->timer->stop(); 76 return; 77 } 78 79 qDebug()<< "start time="<< maxSeconds; 80 m_d->seconds = maxSeconds; 81 m_d->tips = QString("Less than %1 seconds reboot").arg(m_d->seconds); 82 if(m_d->timer == Q_NULLPTR) 83 { 84 m_d->timer = new QTimer(this); 85 m_d->timer->setInterval(1000); 86 connect(m_d->timer, &QTimer::timeout, [=](){ 87 m_d->seconds --; 88 m_d->tips =QString("Less than %1 seconds reboot").arg(m_d->seconds); 89 this->update(); 90 if(m_d->seconds == 0) 91 { 92 m_d->timer->stop(); 93 } 94 }); 95 } 96 97 m_d->timer->start(); 98 this->update(); 99 } 100