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>
AnaPencere mAnapencere;
mAnapencere.setSceneRect(0,0,400,250);
view.setScene(&mAnapencere);
view.show();
return a.exec();
}
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();
}
To copy to clipboard, switch view to plain text mode
anapencere.h :
#ifndef ANAPENCERE_H
#define ANAPENCERE_H
#include <QGraphicsScene>
#include <QTimer>
#include "bargrapher.h"
#include <sys/time.h>
{
Q_OBJECT
public:
explicit AnaPencere
(QObject *parent
= 0);
private:
BarGrapher *grapher;
int mCurrentTime;
int mPrevTime;
int differ;
int fps;
int textPrinter;
struct timeval tv;
private slots:
void updateSlot();
};
#endif // 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
To copy to clipboard, switch view to plain text mode
bargrapher.h
#ifndef BARGRAPHER_H
#define BARGRAPHER_H
#include <QGraphicsItem>
#include <QList>
{
public:
BarGrapher();
void insertData(qreal data );
private:
QList<int> floatingData;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
anapencere.cpp :
#include "anapencere.h"
AnaPencere
::AnaPencere(QObject *parent
) :{
connect(&mTimer,SIGNAL(timeout()),this,SLOT(updateSlot()));
grapher= new BarGrapher();
addItem(grapher);
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;
}
}
#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;
}
}
To copy to clipboard, switch view to plain text mode
bargrapher.cpp
#include "bargrapher.h"
#include <QPainter>
BarGrapher::BarGrapher()
{
}
QRectF BarGrapher
::boundingRect() const {
}
{
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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks