Setting Timer Event for Drawing Ellipse



  • Hello,

    I have to set a timer event in order to draw an circle in the middle, then erase (or delete) the current circle and then draw a new circle as bigger diameter (from inner to outer). I couldnt define the timer event and delete the current circle. Could someone give advice how to organize(write) it correctly. Thank you in advance. The code "grafikspielerei.cpp" ,header file "grafikspielerei.h" and "main.cpp" are included.

    Thank you in advance

    //Grafikspielerei.cpp
    
    #include "grafikspielerei.h"
    #include <QTimer>
    #include <ctime>
    #include <QMessageBox>
    
    Grafikspielerei::Grafikspielerei()
    {
        resize(400, 400);
        setWindowTitle("Grafikspielereien");
    
        timercircle = new QTimer(this);
    
        //connect the timer with the Slot
        QObject::connect(timercircle, SIGNAL(timeout()), this, SLOT(timercircleSlot()));
    }
    
    void Grafikspielerei::paintEvent(QPaintEvent *)
    {
        //rechteck();
        kreise();
    }
    
    
    void Grafikspielerei::kreise()
    {
    
        int x=195;
        int y=195;
        int breite, hoehe;
    
        //create an instance
        QPainter myPainter(this);
    
        //zeiger = new QPainter();
    
    
        breite = width()-390;
        hoehe = height()-390;
    
    
        for (int j = 0; j < 10; j++)
        {
            timercircle->start(1000);
            //QMessageBox::information(this, "Beginning", "Timer startet");
    
            for (int i=0; i < 1; i++)
            {
    
            //drawing the circle
            myPainter.drawEllipse(x, y, breite, hoehe);
    
            }
    
        }
    
        //changing the position of circle center
        x = x - 20;
        y = y - 20;
    
        //width / height of the circle changing from the middle to the outer place
        breite = breite + 40;
        hoehe = hoehe + 40;
        timercircleSlot();
    }
    
    
    void Grafikspielerei::timercircleSlot()
    {
    
        timercircle->stop();
    
    }
    
    
    //Grafikspielerei.h
    
    #ifndef GRAFIKSPIELEREI_H
    #define GRAFIKSPIELEREI_H
    #include <QtGui>
    #include <QWidget>
    #include <QTimer>
    #include <QPushButton>
    
    class Grafikspielerei : public QWidget
    {
        Q_OBJECT
    public:
        Grafikspielerei();
    
        QTimer *timercircle;
        QPainter *zeiger;
    
    
    //die ├╝berschriebene Methode
    protected:
        void paintEvent(QPaintEvent *);
    
    //die Methoden zum Zeichnen
    private:
        void rechteck();
        void linie();
        void linie2();
        void kreis();
        void polygon();
    
    private slots:
        void kreise(); //draw circles
        void timercircleSlot(); //timer for circle
    
    };
    
    #endif // GRAFIKSPIELEREI_H
    
    

    //main.cpp
    #include "grafikspielerei.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Grafikspielerei w;
    w.show();

    return a.exec();
    

    }



  • @cprogcoder sorry if it obvious, but from your code I cannot see where you set the time interval for your timer and where you start it. Have you check QTimer documentation?


  • Lifetime Qt Champion

    Hi,

    Your timer is likely stoped before it can timeout as all the painting will happen faster than one second but even if not, the logic if flowed. You are basically restarting it ten times, it's not a "loop stopper".


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.