#include "pinballwidget.h" #include #include #include #include #include #include class PinBallWidgetPrivate { public: QString number="0"; QString tips; QTimer *timer = Q_NULLPTR; int seconds =0; }; PinBallWidget::PinBallWidget(QWidget *parent) : QWidget(parent), m_d(new PinBallWidgetPrivate) { } PinBallWidget::~PinBallWidget() { delete m_d; } void PinBallWidget::paintEvent(QPaintEvent *e) { Q_UNUSED(e) QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing); QRectF rect = this->rect(); if(!m_d->tips.isEmpty()) { QTextOption o; o.setAlignment(Qt::AlignCenter); QString text = m_d->tips; QFont font = this->font(); font.setBold(true); font.setPointSize(18); painter.setFont(font); QRectF r = painter.boundingRect(rect, text, o); painter.drawText(r, text); return; } QTextOption o; o.setAlignment(Qt::AlignCenter); QString text = QString("Number of dog feeding: %1").arg(m_d->number); QRectF r = painter.boundingRect(rect, text, o); painter.drawText(r, text); } void PinBallWidget::setValue(const QString &number) { m_d->number = number; this->update(); } void PinBallWidget::setTips(const QString &info) { m_d->tips = info; } void PinBallWidget::startDownTime(int maxSeconds) { qDebug()<< "maxSeconds="<< maxSeconds; if(maxSeconds == 0) { m_d->tips.clear(); this->update(); m_d->timer->stop(); return; } qDebug()<< "start time="<< maxSeconds; m_d->seconds = maxSeconds; m_d->tips = QString("Less than %1 seconds reboot").arg(m_d->seconds); if(m_d->timer == Q_NULLPTR) { m_d->timer = new QTimer(this); m_d->timer->setInterval(1000); connect(m_d->timer, &QTimer::timeout, [=](){ m_d->seconds --; m_d->tips =QString("Less than %1 seconds reboot").arg(m_d->seconds); this->update(); if(m_d->seconds == 0) { m_d->timer->stop(); } }); } m_d->timer->start(); this->update(); }