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:
Code:
int main(int argc, char *argv[]) { #include <QApplication> #include "anapencere.h" #include <QGraphicsView> AnaPencere mAnapencere; QGraphicsView view; mAnapencere.setSceneRect(0,0,400,250); view.setScene(&mAnapencere); view.show(); return a.exec(); }
anapencere.h :
Code:
#ifndef ANAPENCERE_H #define ANAPENCERE_H #include <QGraphicsScene> #include <QTimer> #include "bargrapher.h" #include <sys/time.h> { Q_OBJECT public: 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
Code:
#ifndef BARGRAPHER_H #define BARGRAPHER_H #include <QGraphicsItem> #include <QList> { public: BarGrapher(); void insertData(qreal data ); private: QList<int> floatingData; }; #endif // BARGRAPHER_H
anapencere.cpp :
Code:
#include "anapencere.h" { 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){ textPrinter=0; } }
bargrapher.cpp
Code:
#include "bargrapher.h" #include <QPainter> BarGrapher::BarGrapher() { } { } { painter->setPen(Qt::black); 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); }