Results 1 to 9 of 9

Thread: Problem in blinking

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Problem in blinking

    Hello, in this program I want to blinking of the rectangle.in this way: First, Blue rectangle show.and after 500 msec,Red rectangle show.but, when I run this program,I see A high pause and then red rectangle show and Not Blink well.
    Qt Code:
    1. #include "lines.h"
    2. #include <QPainter>
    3. #include <QThread>
    4. Lines::Lines(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7.  
    8. }
    9.  
    10. void Lines::paintEvent(QPaintEvent *e)
    11. {
    12. for(int i=1;i<=10;i++)
    13. {
    14. Q_UNUSED(e);
    15.  
    16. QPainter qp(this);
    17. QBrush brush(Qt::blue);
    18. qp.setBrush(brush);
    19. qp.drawRect(100,100,200,200);
    20. QThread::msleep(500);
    21. brush.setColor(Qt::red);
    22. qp.setBrush(brush);
    23. qp.drawRect(100,100,200,200);
    24. }}
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in blinking

    paintEvent should be used to present the current state of the widget, if you are expecting to get some kind of animation with this "for" loop then it is not going to happen. Simplest way would be to
    1. create a custom slot where you change a "color" member variable and call update()
    2. call this slot periodically using a timer
    3. reimplement paintEvent and paint the widget using current "color" value

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem in blinking

    See also my answer (and others) to your earlier thread about delays: here

    Stampede has nailed the Qt approach to this problem.

  4. #4
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Problem in blinking

    thank you very much.I changed the above code, As follows:But didn't work.
    Qt Code:
    1. #include "lines.h"
    2. #include <QPainter>
    3. #include <QTimer>
    4. Lines::Lines(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7.  
    8. }
    9.  
    10. void Lines::paintEvent(QPaintEvent *e)
    11. {Q_UNUSED(e);
    12. QPainter qp(this);
    13. red(&qp);
    14. QTimer *timer = new QTimer(this);
    15. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    16. timer->start(1000);
    17. blue(&qp);
    18. }
    19.  
    20. void Lines::red(QPainter*qp)
    21. {
    22. QBrush brush(Qt::blue);
    23. qp->setBrush(brush);
    24. qp->drawRect(200,200,200,200);
    25. }
    26. void Lines::blue(QPainter*pa)
    27. {
    28.  
    29. QBrush brush(Qt::red);
    30. pa->setBrush(brush);
    31. pa->drawRect(200,200,200,200);
    32. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem in blinking

    So you are painting a red rectangle into the buffer of the widget and then, immediately afterwards, a blue rectangle.
    So you see a blue rectangle.

    A pixel can always only have one color, painting blue on a red rectangle makes it a blue rectangle.

    You are also creating a new timer every time the paintEvent() method is called, which is every time the widget has to repaint itself.

    If you look at your painting code, the only difference is the color.
    So why don't you simply store the "current" color in a member variable, use that when paintEvent() is called and let the slot that is connected to the timer change the color in that variable.

    Btw, if you are looking for a good place to create a single instance of a timer, you should be looking for a method that is guaranteed to be only called once per object instance.

    Cheers,
    _

  6. #6
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Problem in blinking

    thank you.please display me the important part of the above code that it have not it.I know c++, but not Professional.please display the correct code.I tried but I could not.I'm really sorry.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in blinking

    Your problem is not really about knowing or not knowing C++, be it on amateur or professional level. Your problem is you are not planning what you want to do and you do not look for means of realizing your plan. You must devote some time to reading documentation or else you will just be blindly trying different things until maybe eventually hit the right approach by trial and error. It seems you have a hard time understanding what paintEvent is for and how event-driven processing works. This is all explained in the numerous tutorals and examples that come with Qt. I suggest that instead of downloading random applications from the internet you rather work with the examples Qt release provides and learn from them until you are able to do things on your own.

    Right now you already have almost 100 posts on this forum but still you are unable to do even the simplest things.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Problem in blinking

    I changed the above code, As follows: and only I want to paint a rectangle by blue and I want to delay 1000 msec for it and also such as Traffic Lights, It is Blinking

    Qt Code:
    1. #include <QWidget>
    2. class Lines : public QWidget
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. Lines(QWidget *parent = 0);
    8.  
    9. protected:
    10. void paintEvent(QPaintEvent *event);
    11.  
    12.  
    13. public slots:
    14. void blue(QPainter*pa);
    15. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "lines.h"
    2. #include <QPainter>
    3. #include <QTimer>
    4. Lines::Lines(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. QTimer *timer = new QTimer(this);
    8. connect(timer, SIGNAL(timeout()), this, SLOT(blue(QPainter*pa)));
    9. timer->start(1000);
    10.  
    11. }
    12.  
    13. void Lines::paintEvent(QPaintEvent *e)
    14. {Q_UNUSED(e);
    15. QPainter qp(this);
    16. blue(&qp);
    17. }
    18.  
    19. void Lines::blue(QPainter*pa)
    20. {
    21.  
    22. QBrush brush(Qt::blue);
    23. pa->setBrush(brush);
    24. pa->drawRect(200,200,200,200);
    25. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 15 minutes:


    Mr anda skoa, Mr wysota and other Mr, please help me.this post is not a spam.
    Last edited by rezas1000; 30th September 2014 at 12:56.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in blinking

    Quote Originally Posted by rezas1000 View Post
    I changed the above code, As follows: and only I want to paint a rectangle by blue and I want to delay 1000 msec for it and also such as Traffic Lights, It is Blinking
    Your code certainly does not do what you say it does. It simply writes a warning to the console and paints the widget blue.

    Here is a quick exercise for you. Subclass QWidget, reimplement its paintEvent() but do not call it anywhere. Then in main create and show the widget. See if your paintEvent() gets executed or not. What are the conclusions?

    Qt Code:
    1. #include <QWidget>
    2. #include <QApplication>
    3. #include <QtDebug>
    4.  
    5. class Widget : public QWidget {
    6. public:
    7. Widget() {}
    8. protected:
    9. void paintEvent(QPaintEvent *) {
    10. qDebug() << "This is written from paint event";
    11. }
    12. };
    13.  
    14. int main(int argc, char **argv) {
    15. QApplication app(argc, argv);
    16. Widget w;
    17. w.show();
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. blinking
    By madhukumar in forum Qt Programming
    Replies: 3
    Last Post: 20th November 2013, 10:09
  2. Blinking problem with QListWidget with batch
    By visiray in forum Qt Programming
    Replies: 0
    Last Post: 12th December 2010, 18:35
  3. Blinking Buttons
    By metdos in forum Newbie
    Replies: 7
    Last Post: 22nd July 2009, 15:36
  4. Blinking QGraphicsItem
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2007, 01:33

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.