Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

CircularCountDown QWidget



  • Having problem creating a CircularCountDown QWidget class

    #ifndef CIRCULARCOUNTDOWN_H
    #define CIRCULARCOUNTDOWN_H
    #include <QWidget>
    #include <QtCore>
    #include <QSize>
    #include <QtGui>
    class CircularCountdown : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit CircularCountdown(int msec =5000, QWidget *parent =0 );
        ~CircularCountdown();
    
        void setDuration(int msec);
        int duration() const;
    public slots:
        void start();
        void stop();
        void pause();
        void resume();
    signals:
        void timeoutReached();
    protected:
        QSize sizeHint() const;
        void paintEvent(QPaintEvent *event);
    
    private:
        class Private;
        Private * const d;
    };
    #endif // CIRCULARCOUNTDOWN_H
    
    #include "circularcountdown.h"
    #include <QPaintEvent>
    #include <QTimeLine>
    #include <QPainter>
    
    
    class CircularCountdown::Private
    {
    public:
       Private(CircularCountdown *parent)
           : q(parent)
       {
    
       }
    
       CircularCountdown *q;
       QTimeLine *timeLine;
    };
    
    CircularCountdown::CircularCountdown(int msec, QWidget *parent):QWidget(parent), d(new Private(this))
    {
    
        setAutoFillBackground(false);
        d->timeLine->setFrameRange(0,720);
        d->timeLine->setDirection(QTimeLine::Backward);
    
        connect(d->timeLine,SIGNAL(frameChanged(int)), this, SLOT(repaint()));
        connect(d->timeLine,SIGNAL(finished()),this,SLOT(repaint()));
        connect(d->timeLine,SIGNAL(finished()),this, SIGNAL(timeoutReached()));
    }
    
    CircularCountdown::~CircularCountdown()
    {
    
    }
    
    void CircularCountdown::start()
    {
        d->timeLine->start();
    }
    
    void CircularCountdown::stop()
    {
        d->timeLine->stop();
    }
    
    
    
    void CircularCountdown::pause()
    {
    
        d->timeLine->setPaused(true);
    }
    
    void CircularCountdown::resume()
    {
        d->timeLine->setPaused(true);
    }
    
    
    
    QSize CircularCountdown::sizeHint() const
    {
        return QSize(16,16);
    }
    
    
    void CircularCountdown::paintEvent(QPaintEvent *event)
    {
        if (d->timeLine->state() == QTimeLine::Running || d->timeLine->state() == QTimeLine::Paused) {
               QPainter painter(this);
               painter.setBrush(QBrush(parentWidget()->palette().color(QPalette::Base),Qt::SolidPattern));
               painter.setRenderHint(QPainter::Antialiasing);
               painter.drawPie(this->rect().adjusted(2,2,-2,-2),90*16, -d->timeLine->currentFrame()*8);
    
               }
    }
    

  • Moderators

    @alvinNew Please use ``` (3 backticks) for posting code blocks. Otherwise it is difficult for others to read it.


Log in to reply