Results 1 to 8 of 8

Thread: How to connect QTimer with QPaintEvent?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2012
    Posts
    9
    Platforms
    Windows Symbian S60

    Exclamation How to connect QTimer with QPaintEvent?

    Good Evening!

    Is it possible to connect the Qtimer with QPaintEvent. In my project I have already created the QPaintEvent, and drew the Ellipse, now I want to create the qtimer and after timeout I want that my existing ellipse will move to another place on my widget. Is it possible to make that? I have made it but my ellipse does not move. Please help me. I really hope on your help! Thanks!
    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Widget(QWidget *parent = 0);
    12. ~Widget();
    13.  
    14. QGridLayout *grid;
    15. QWidget *widget1;
    16. //QPen *pen;
    17. QBrush *brush, *brush1;
    18. QTimer *timer;
    19.  
    20. public slots:
    21. void paintEvent(QPaintEvent *);
    22. void paintEvent1(QPaintEvent *);
    23.  
    24. };
    25.  
    26. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "widget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2.  
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. grid = new QGridLayout;
    7. widget1= new QWidget;
    8.  
    9. timer = new QTimer(this);
    10.  
    11. grid->addWidget(widget1,0,6,0,6);
    12. this->setLayout(grid);
    13.  
    14.  
    15. QPainter painter(this);
    16. //pen = new QPen;
    17. brush = new QBrush;
    18. brush1= new QBrush;
    19. //pen->setColor(Qt::blue);
    20. brush->setColor(Qt::red);
    21.  
    22.  
    23. connect(timer, SIGNAL(timeout()), this,SLOT(paintEvent1(QPaintEvent*)));
    24. timer->start(5);
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32. }
    33.  
    34. Widget::~Widget()
    35. {
    36.  
    37. }
    38. void Widget::paintEvent(QPaintEvent *)
    39. {
    40.  
    41. QPainter painter(this);
    42.  
    43.  
    44. painter.setBrush(Qt::SolidPattern);
    45. /*QRadialGradient radialGradient(50, 50, 50, 70, 70);
    46.   radialGradient.setColorAt(0.0, Qt::white);
    47.   radialGradient.setColorAt(0.2, Qt::green);
    48.   radialGradient.setColorAt(1.0, Qt::black);
    49.   painter.setBrush(radialGradient);*/
    50. painter.drawEllipse(0,0,125,125);
    51.  
    52.  
    53. update();
    54.  
    55.  
    56.  
    57. }
    58. void Widget::paintEvent1(QPaintEvent *)
    59. {
    60.  
    61. QPainter painter1(this);
    62. painter1.setBrush(Qt::SolidPattern);
    63. painter1.drawEllipse(0,0,185,185);
    64. update();
    65.  
    66.  
    67. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Asus_G72GX; 1st May 2012 at 15:25.

Similar Threads

  1. QMouseEvent on QPaintEvent
    By GUIman in forum Newbie
    Replies: 7
    Last Post: 1st March 2011, 08:51
  2. How to sendEvent QPaintEvent ?
    By SABROG in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2009, 21:55
  3. Can we connect QTimer::SingleShot with a slot taking arguments?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2008, 18:02
  4. QPaintEvent on button click?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 08:44
  5. Inheritance and QpaintEvent
    By djoul in forum Qt Programming
    Replies: 22
    Last Post: 5th July 2006, 13:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.