Results 1 to 3 of 3

Thread: Flickering problem

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Flickering problem

    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:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. #include <QApplication>
    4. #include "anapencere.h"
    5. #include <QGraphicsView>
    6.  
    7. QApplication a(argc, argv);
    8. AnaPencere mAnapencere;
    9. mAnapencere.setSceneRect(0,0,400,250);
    10. view.setScene(&mAnapencere);
    11. view.show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    anapencere.h :
    Qt Code:
    1. #ifndef ANAPENCERE_H
    2. #define ANAPENCERE_H
    3.  
    4. #include <QGraphicsScene>
    5. #include <QTimer>
    6. #include "bargrapher.h"
    7. #include <sys/time.h>
    8.  
    9. class AnaPencere : public QGraphicsScene
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit AnaPencere(QObject *parent = 0);
    14.  
    15. private:
    16. QTimer mTimer;
    17. BarGrapher *grapher;
    18. int mCurrentTime;
    19. int mPrevTime;
    20. int differ;
    21. int fps;
    22. int textPrinter;
    23. struct timeval tv;
    24.  
    25. private slots:
    26. void updateSlot();
    27.  
    28. };
    29.  
    30. #endif // ANAPENCERE_H
    To copy to clipboard, switch view to plain text mode 

    bargrapher.h
    Qt Code:
    1. #ifndef BARGRAPHER_H
    2. #define BARGRAPHER_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QList>
    6.  
    7. class BarGrapher : public QGraphicsItem
    8. {
    9. public:
    10. BarGrapher();
    11. QRectF boundingRect() const;
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    13. void insertData(qreal data );
    14. private:
    15. QList<int> floatingData;
    16. };
    17.  
    18. #endif // BARGRAPHER_H
    To copy to clipboard, switch view to plain text mode 

    anapencere.cpp :
    Qt Code:
    1. #include "anapencere.h"
    2.  
    3. AnaPencere::AnaPencere(QObject *parent) :
    4. {
    5. connect(&mTimer,SIGNAL(timeout()),this,SLOT(updateSlot()));
    6. grapher= new BarGrapher();
    7. addItem(grapher);
    8. fpsText= new QGraphicsTextItem;
    9. fpsText->setPlainText("0");
    10. addItem(fpsText);
    11. mPrevTime=0;
    12. mCurrentTime=0;
    13. textPrinter=0;
    14. mTimer.start(40);
    15. }
    16.  
    17. void AnaPencere::updateSlot()
    18. {
    19. grapher->insertData(rand()%200);
    20. grapher->update();
    21.  
    22. textPrinter++;
    23. gettimeofday(&tv,NULL);
    24. mPrevTime=mCurrentTime;
    25. mCurrentTime = tv.tv_sec % 100000 * 1000;
    26. mCurrentTime += tv.tv_usec/1000 ;
    27. differ= mCurrentTime-mPrevTime;
    28. if(differ!=0)
    29. fps=1000/(differ);
    30. else fps=1000;
    31.  
    32. if(textPrinter%100==0){
    33. fpsText->setPlainText(QString::number(fps)+" fps");
    34. textPrinter=0;
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    bargrapher.cpp
    Qt Code:
    1. #include "bargrapher.h"
    2. #include <QPainter>
    3.  
    4. BarGrapher::BarGrapher()
    5. {
    6. }
    7. QRectF BarGrapher::boundingRect() const
    8. {
    9. return QRectF(0, 0,400, 250);
    10. }
    11.  
    12. void BarGrapher::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    13. QWidget *widget)
    14. {
    15. painter->setPen(Qt::black);
    16. painter->setBrush(QBrush(Qt::red));
    17. painter->fillRect(QRect(0,0,400,250),Qt::green);
    18. for(int i=0;i<floatingData.size();++i)
    19. {
    20. painter->drawRect(0,floatingData.at(i),10,300);
    21. painter->translate(10,0);
    22. }
    23. if(floatingData.size()>=50)
    24. floatingData.takeFirst();
    25.  
    26. }
    27.  
    28. void BarGrapher::insertData(qreal data)
    29. {
    30. floatingData.append((int)data);
    31. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Flickering problem

    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..

  3. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Flickering problem

    I havent used drawing from cache before. Is there any reference example for that?

Similar Threads

  1. GraphicsItem is flickering
    By anafor2004 in forum Qt Programming
    Replies: 3
    Last Post: 30th September 2009, 11:44
  2. Flickering
    By Pembar in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2009, 19:21
  3. QtOgreFramework flickering with qt 4.5
    By Angelo Moriconi in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2009, 10:52
  4. flickering x-scale
    By sun in forum Qwt
    Replies: 2
    Last Post: 1st October 2008, 07:57
  5. QFileDialog flickering
    By invictus in forum Newbie
    Replies: 8
    Last Post: 17th April 2008, 09:47

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.