This will trigger paint event.Qt Code:
connect( timer, SIGNAL( timeout() ), widget, SLOT( update() ) );To copy to clipboard, switch view to plain text mode
Btw I hope that you're not moving your ellipse in the paint event...
This will trigger paint event.Qt Code:
connect( timer, SIGNAL( timeout() ), widget, SLOT( update() ) );To copy to clipboard, switch view to plain text mode
Btw I hope that you're not moving your ellipse in the paint event...
Thank you for reply! I have remake code. But Qt started to ignore qtimer, 2nd ellipse is drowing immediately. Is there any possibility to destroy first ellipse after drawing 2nd? Or maybe it is impossible in QPaintEvent. Maybe try to do some stuff with QPoint?
Qt Code:
#include "widget.h" { grid->addWidget(widget1,0,6,0,6); this->setLayout(grid); //pen = new QPen; //pen->setColor(Qt::blue); brush->setColor(Qt::red); connect(timer, SIGNAL(timeout()), widget1,SLOT(update())); timer->start(20); } Widget::~Widget() { } { painter.setBrush(Qt::SolidPattern); /*QRadialGradient radialGradient(50, 50, 50, 70, 70); radialGradient.setColorAt(0.0, Qt::white); radialGradient.setColorAt(0.2, Qt::green); radialGradient.setColorAt(1.0, Qt::black); painter.setBrush(radialGradient);*/ painter.drawEllipse(0,0,125,125); update(); } void Widget::update() { painter.drawEllipse(0,0,185,185); }To copy to clipboard, switch view to plain text mode
Header.h
Qt Code:
#ifndef WIDGET_H #define WIDGET_H #include <QtGui> { Q_OBJECT public: ~Widget(); QGridLayout *grid; QWidget *widget1; //QPen *pen; QTimer *timer; public slots: void update(); }; #endif // WIDGET_HTo copy to clipboard, switch view to plain text mode
Some thoughts:
- QWidget::update() is a slot your widget already has, not something you need to reimplement.
- Calling update() inside the paintEvent() makes no sense.
- Connecting the timer to a generic QWidget (that you are not painting) will not cause your widget to paint.
Here is a complete example of a custom widget: Analogue Clock Example
Last edited by ChrisW67; 2nd May 2012 at 07:28.
some more points:
connect(timer, SIGNAL(timeout()), this,SLOT(paintEvent1(QPaintEvent*)));
you cant connect signals with X number of arguments to to slots with more than X arguments.
You shouldn't be making multiple paint event methods. THE paintEvent will always get called on update, YOUR paint event will only get called on timeout. Why should there be two? (There definitely should not be)
You don't need to 'destroy' anything in paint event - paint event always starts with a clean canvas.
It is only doing what you tell it to do. You call update() in paintevent. Your misguided addition of your own update() method draws the second ellipse at the same time as the first. Please read your own code more carefully.But Qt started to ignore qtimer, 2nd ellipse is drowing immediately.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
I'm in a good mood today, so I'll give you ready-made (compilable) solution, if this isn't enough, then you need to go back to the books (you should anyway, most of stuff guys above are saying is in the documentation)
Qt Code:
// header #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui/QMainWindow> { Q_OBJECT public: public slots: void redrawEllipse( void ); protected: private: int radius; }; #endif // MAINWINDOW_HTo copy to clipboard, switch view to plain text mode
Qt Code:
// implementation #include "mainwindow.h" #include <QPainter> #include <QTimer> : radius( 1 ) { connect( t, SIGNAL( timeout() ), this, SLOT( redrawEllipse() ) ); t->start( 10 ); } void MainWindow::redrawEllipse( void ) { radius += 2; if( radius > qMin( this->width()/2, this->height()/2 ) ) { radius = 1; } this->update(); } { Q_UNUSED( e ); p.drawEllipse( this->rect().center(), this->radius, this->radius ); }To copy to clipboard, switch view to plain text mode
Spitfire great thanks for you that you helped me with this!
Anyway I have discussed this problem with my lector he showed me that it possiblee to move ellipse if we declare a coordinates for it like a dx ,dy,x,y.
The final result must be close to this images below:
But I have collided that Qt does not see a z coordinate. So I make them by x,y. My code result is close to those sample images, but I was not able to move the ball to the right and upwards. Maybe is it possible to change the values? Thanks again!
Widget.h
Qt Code:
#ifndef WIDGET_H #define WIDGET_H #include <QtGui> { Q_OBJECT public: ~Widget(); QGridLayout *grid; //QWidget *widget1; QPen *pen; //QTimer *timer; QMatrix matrix; int x; int x2; int y; int y2; int dx, dy; int dx2, dy2; public slots: void move(); }; #endif // WIDGET_HTo copy to clipboard, switch view to plain text mode
Main.cpp
Qt Code:
#include <QtGui/QApplication> #include "widget.h" int main(int argc, char *argv[]) { Widget w; w.show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
Widget.cpp
Qt Code:
#include "widget.h" { //widget1= new QWidget; //grid->addWidget(widget1,0,6,0,6); this->setLayout(grid); //pen->setColor(Qt::blue); brush->setColor(Qt::red); //timer = new QTimer(this); /*x = 10; dx = 5; dy = 10; y =30;*/ x = 10; dx = 5; dy = 10; y =30; x2 = 10; dx2 = 5; dy2 = 10; y2 =30; //connect(timer, SIGNAL(timeout()), this,SLOT(move())); //timer->start(3000); this->startTimer(100); } x = x + dx; if(x >=165) dx = -5; if(x <0) dx = 5; y = y + dy; if(y <=-165) dy = 10; if(y >0) dy = -10; // 2nd leg for blue ball x2 = x2+ dx2; if(x2 >=165) dx2 = 5; if(x2 <0) dx2 = -5; if(x2>5) dx2=1; y2 = y2- dy2; if(y2 <=-165) dy2 = 10; if(y2 >0) dy2 = -10; if(y2>-10) dx2=15; /*if(y2>=100) dy2=5; if(y2>0) dy2=-5;*/ /* y2 = y2 + dy2; if(y2 <=100) dy2 = 1; if(y2 >0) dy2 = -1;*/ update(); } Widget::~Widget() { } { painter.setBrush(Qt::red); /*QRadialGradient radialGradient(50, 50, 50, 70, 70); radialGradient.setColorAt(0.0, Qt::white); radialGradient.setColorAt(0.2, Qt::green); radialGradient.setColorAt(1.0, Qt::black); painter.setBrush(radialGradient);*/ painter.drawEllipse(x,y,40,40); painter.setBrush(Qt::blue); painter.drawEllipse(x2,y2,10,10); } void Widget::move() { painter.drawEllipse(0,0,165,165); }To copy to clipboard, switch view to plain text mode
You should be using OO approach.
Create a class 'ball', give it what you need and then move it around.
Parent widget can be canvas for the ball to move around.
To move ball in oposite direction, just multiply dx and/or dy by -1 every time you hit some edge.
here's example ball class:
Don't come back if you won't be able to figure out how to use it...Qt Code:
{ Q_OBJECT public: : dx( dx ), dy( dy ) { this->setFixedSize( 20, 20 ); } public slots: void advance( void ) { int x = p.x() + this->dx; if( x < 0 ) { x = 0; this->dx *= -1; } else if( x + this->width() > this->parentWidget()->width() ) { x = this->parentWidget()->width() - this->width(); this->dx *= -1; } int y = p.y() + this->dy; if( y < 0 ) { y = 0; this->dy *= -1; } else if( y + this->height() > this->parentWidget()->height() ) { y = this->parentWidget()->height() - this->height(); this->dy *= -1; } this->move( x, y ); } protected: { Q_UNUSED( e ); QPainterPath pp; pp.addEllipse( this->rect() ); p.fillPath( pp, Qt::red ); } private: int dx; int dy; };To copy to clipboard, switch view to plain text mode![]()
Bookmarks