PDA

View Full Version : Flickering problem



yagabey
5th May 2010, 08:12
Hello,
I want to visualize a flowing data with a bar graph. Ive written a test code; but i see some flickers when the graph is being drawn. I know that double buffering is default with qt4. What should I do else to remove the flickers on the screen? The code is below:

main.cpp:

int main(int argc, char *argv[])
{
#include <QApplication>
#include "anapencere.h"
#include <QGraphicsView>

QApplication a(argc, argv);
AnaPencere mAnapencere;
QGraphicsView view;
mAnapencere.setSceneRect(0,0,400,250);
view.setScene(&mAnapencere);
view.show();
return a.exec();
}

anapencere.h :

#ifndef ANAPENCERE_H
#define ANAPENCERE_H

#include <QGraphicsScene>
#include <QTimer>
#include "bargrapher.h"
#include <sys/time.h>

class AnaPencere : public QGraphicsScene
{
Q_OBJECT
public:
explicit AnaPencere(QObject *parent = 0);

private:
QTimer mTimer;
BarGrapher *grapher;
QGraphicsTextItem *fpsText;
int mCurrentTime;
int mPrevTime;
int differ;
int fps;
int textPrinter;
struct timeval tv;

private slots:
void updateSlot();

};

#endif // ANAPENCERE_H

bargrapher.h

#ifndef BARGRAPHER_H
#define BARGRAPHER_H

#include <QGraphicsItem>
#include <QList>

class BarGrapher : public QGraphicsItem
{
public:
BarGrapher();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void insertData(qreal data );
private:
QList<int> floatingData;
};

#endif // BARGRAPHER_H

anapencere.cpp :

#include "anapencere.h"

AnaPencere::AnaPencere(QObject *parent) :
QGraphicsScene(parent)
{
connect(&mTimer,SIGNAL(timeout()),this,SLOT(updateSlot()));
grapher= new BarGrapher();
addItem(grapher);
fpsText= new QGraphicsTextItem;
fpsText->setPlainText("0");
addItem(fpsText);
mPrevTime=0;
mCurrentTime=0;
textPrinter=0;
mTimer.start(40);
}

void AnaPencere::updateSlot()
{
grapher->insertData(rand()%200);
grapher->update();

textPrinter++;
gettimeofday(&tv,NULL);
mPrevTime=mCurrentTime;
mCurrentTime = tv.tv_sec % 100000 * 1000;
mCurrentTime += tv.tv_usec/1000 ;
differ= mCurrentTime-mPrevTime;
if(differ!=0)
fps=1000/(differ);
else fps=1000;

if(textPrinter%100==0){
fpsText->setPlainText(QString::number(fps)+" fps");
textPrinter=0;
}
}

bargrapher.cpp

#include "bargrapher.h"
#include <QPainter>

BarGrapher::BarGrapher()
{
}
QRectF BarGrapher::boundingRect() const
{
return QRectF(0, 0,400, 250);
}

void BarGrapher::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->setPen(Qt::black);
painter->setBrush(QBrush(Qt::red));
painter->fillRect(QRect(0,0,400,250),Qt::green);
for(int i=0;i<floatingData.size();++i)
{
painter->drawRect(0,floatingData.at(i),10,300);
painter->translate(10,0);
}
if(floatingData.size()>=50)
floatingData.takeFirst();

}

void BarGrapher::insertData(qreal data)
{
floatingData.append((int)data);
}

aamer4yu
5th May 2010, 12:25
What if you reduce the timer from 40ms to 20 or 25ms ?
40 ms will give 25fps speed, but if your drawing takes some time, then 25 fps wont be acheivable, which is why you might be facing flickering.

If that doesnt work, try caching the drawing a image on timeout, and in paint() simply draw from that cache..

yagabey
5th May 2010, 13:25
I havent used drawing from cache before. Is there any reference example for that?