Results 1 to 3 of 3

Thread: How to efficently fade out large QGraphicsItem.

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default How to efficently fade out large QGraphicsItem.

    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 

  2. #2
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to efficently fade out large QGraphicsItem.

    How about writing QGraphicsRectItem subclass and reimplementing its paint() function like this:

    Qt Code:
    1. class MyRectItem : public QGraphicsRectItem
    2. {
    3. public:
    4. void paint(QPainter *p, ....);
    5. void setOpacity(qreal o) {mOpacity = o};
    6. private:
    7. qreal mOpacity;
    8. }
    9. MyRectItem::paint( QPainter *p .... )
    10. {
    11. p->save();
    12. p->setOpacity(mOpacity);
    13. QGraphicsRectItem::paint(p, ....);
    14. p->restore();
    15. }
    To copy to clipboard, switch view to plain text mode 

    and then calling setOpacity() from your animate() in scene and updating item?

    I'm using that technique in one of my apps

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to efficently fade out large QGraphicsItem.

    I get the same result. The larger the window, the more flicker can be seen. The animation is cpu-intensive but works fine. When I write on a GLWidget even that problem isn't an issue but both ways I get annoying flicker .

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.