PDA

View Full Version : Problem in blinking



rezas1000
26th September 2014, 21:10
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.

#include "lines.h"
#include <QPainter>
#include <QThread>
Lines::Lines(QWidget *parent)
: QWidget(parent)
{

}

void Lines::paintEvent(QPaintEvent *e)
{
for(int i=1;i<=10;i++)
{
Q_UNUSED(e);

QPainter qp(this);
QBrush brush(Qt::blue);
qp.setBrush(brush);
qp.drawRect(100,100,200,200);
QThread::msleep(500);
brush.setColor(Qt::red);
qp.setBrush(brush);
qp.drawRect(100,100,200,200);
}}

stampede
26th September 2014, 21:17
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

ChrisW67
26th September 2014, 22:58
See also my answer (and others) to your earlier thread about delays: here

Stampede has nailed the Qt approach to this problem.

rezas1000
29th September 2014, 18:29
thank you very much.I changed the above code, As follows:But didn't work.

#include "lines.h"
#include <QPainter>
#include <QTimer>
Lines::Lines(QWidget *parent)
: QWidget(parent)
{

}

void Lines::paintEvent(QPaintEvent *e)
{Q_UNUSED(e);
QPainter qp(this);
red(&qp);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
blue(&qp);
}

void Lines::red(QPainter*qp)
{
QBrush brush(Qt::blue);
qp->setBrush(brush);
qp->drawRect(200,200,200,200);
}
void Lines::blue(QPainter*pa)
{

QBrush brush(Qt::red);
pa->setBrush(brush);
pa->drawRect(200,200,200,200);
}

anda_skoa
29th September 2014, 18:47
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,
_

rezas1000
29th September 2014, 22:02
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.

wysota
29th September 2014, 22:09
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.

rezas1000
30th September 2014, 12:56
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


#include <QWidget>
class Lines : public QWidget
{
Q_OBJECT

public:
Lines(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);


public slots:
void blue(QPainter*pa);
};


#include "lines.h"
#include <QPainter>
#include <QTimer>
Lines::Lines(QWidget *parent)
: QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(blue(QPainter*pa)));
timer->start(1000);

}

void Lines::paintEvent(QPaintEvent *e)
{Q_UNUSED(e);
QPainter qp(this);
blue(&qp);
}

void Lines::blue(QPainter*pa)
{

QBrush brush(Qt::blue);
pa->setBrush(brush);
pa->drawRect(200,200,200,200);
}


Added after 1 15 minutes:

Mr anda skoa, Mr wysota and other Mr, please help me.this post is not a spam.

wysota
30th September 2014, 13:54
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?


#include <QWidget>
#include <QApplication>
#include <QtDebug>

class Widget : public QWidget {
public:
Widget() {}
protected:
void paintEvent(QPaintEvent *) {
qDebug() << "This is written from paint event";
}
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}