Hi everybody and excuse me for asking so many questions lately

I have a QGraphicsRectItem that covers the whole QGraphicsView. I want to fade it out smoothly in about 600-1000 milliseconds. The problem is that it is flaky, even with QGLWidget as viewport. Can anybody tell me how to improve the performance and get rid of the annoying flicker?

Needs the QtOpenGL module to compile
Qt Code:
  1. #include <QApplication>
  2. #include <QtGui>
  3. #include <QtOpenGL>
  4.  
  5. class GraphicsScene : public QGraphicsScene
  6. {
  7. Q_OBJECT
  8. public:
  9. GraphicsScene(QObject *parent=0)
  10. {
  11. item = 0;
  12. timeLine = 0;
  13. }
  14. private:
  15. QTimeLine * timeLine;
  16. public:
  17. void initAnimation()
  18. {
  19. if (item == 0) {
  20. item = new QGraphicsRectItem;
  21. item->setRect(sceneRect());
  22. addItem(item);
  23. item->hide();
  24. }
  25. if (timeLine == 0) {
  26. timeLine = new QTimeLine(750, this);
  27. connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
  28. }
  29. timeLine->start();
  30. }
  31. public slots:
  32. void onViewRectChanged(QRectF r)
  33. {
  34. setSceneRect(r);
  35. if (item)
  36. item->setRect(r);
  37. }
  38. private slots:
  39. void animate(qreal val)
  40. {
  41. (val < 1) ? item->show() : item->hide();
  42. item->setPen(Qt::NoPen);
  43. item->setBrush(QColor(0, 0, 0, (1-val)*255));
  44. }
  45. };
  46.  
  47. class MainWindow : public QMainWindow
  48. {
  49. Q_OBJECT
  50. public:
  51. MainWindow(QWidget *parent=0)
  52. {
  53. scene = new GraphicsScene();
  54. view = new QGraphicsView(scene);
  55. if (QGLFormat::hasOpenGL()) {
  56. view->setViewport(new QGLWidget);
  57. }
  58. view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  59. view->setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
  60. view->installEventFilter(this);
  61. connect(this, SIGNAL(viewRectChanged(QRectF)), scene, SLOT(onViewRectChanged(QRectF)));
  62.  
  63. setCentralWidget(view);
  64. }
  65. virtual bool eventFilter(QObject *obj, QEvent *event)
  66. {
  67. if (obj == view && event->type() == QEvent::Resize) {
  68. emit viewRectChanged(view->contentsRect());
  69. }
  70. return QMainWindow::eventFilter(obj, event);
  71. }
  72. protected:
  73. void keyPressEvent(QKeyEvent *event)
  74. {
  75. if (event->key() == Qt::Key_Space)
  76. scene->initAnimation();
  77. }
  78.  
  79. private:
  80. GraphicsScene *scene;
  81. signals:
  82. void viewRectChanged(QRectF);
  83. };
  84.  
  85. int main(int argc, char **argv)
  86. {
  87. QApplication app(argc, argv);
  88. MainWindow mw;
  89. mw.resize(1024, 768);
  90. mw.show();
  91. return app.exec();
  92. }
  93. #include "main.moc"
To copy to clipboard, switch view to plain text mode