Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting Timer Event for Drawing Ellipse
Forum Updated to NodeBB v4.3 + New Features

Setting Timer Event for Drawing Ellipse

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 913 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cprogcoder
    wrote on last edited by
    #1

    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();
    

    }

    1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @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?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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".

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved