Results 1 to 10 of 10

Thread: QOpenGL QTimer, how do I animate this?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: QOpenGL QTimer, how do I animate this?

    Use QOpenGLWindow with the frameSwapped signal. This is my example with Qt 6.7 that prints delta time to the console:

    main.cpp

    Qt Code:
    1. #include <QtCore/QElapsedTimer>
    2. #include <QtGui/QOpenGLFunctions>
    3. #include <QtGui/QSurfaceFormat>
    4. #include <QtOpenGL/QOpenGLWindow>
    5. #include <QtWidgets/QApplication>
    6.  
    7. class OpenGLWindow : public QOpenGLWindow, private QOpenGLFunctions
    8. {
    9. public:
    10.  
    11. OpenGLWindow()
    12. {
    13. setTitle("OpenGL ES 2.0, Qt6, C++");
    14. resize(380, 380);
    15.  
    16. QSurfaceFormat surfaceFormat;
    17. surfaceFormat.setSwapInterval(1);
    18. connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
    19. setFormat(surfaceFormat);
    20. }
    21.  
    22. private:
    23.  
    24. void initializeGL() override
    25. {
    26. initializeOpenGLFunctions();
    27. glClearColor(0.2f, 0.2f, 0.2f, 1.f);
    28. m_elapsedTimer.start();
    29. }
    30.  
    31. void resizeGL(int w, int h) override
    32. {
    33. glViewport(0, 0, w, h);
    34. }
    35.  
    36. void paintGL() override
    37. {
    38. float dt = m_elapsedTimer.elapsed() / 1000.f;
    39. m_elapsedTimer.restart();
    40. qDebug() << dt;
    41. glClear(GL_COLOR_BUFFER_BIT);
    42. }
    43.  
    44. private:
    45. QElapsedTimer m_elapsedTimer;
    46. };
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QApplication::setAttribute(Qt::ApplicationAttribute::AA_UseDesktopOpenGL);
    51. QApplication app(argc, argv);
    52. OpenGLWindow w;
    53. w.show();
    54. return app.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2024
    Posts
    1
    Qt products
    Qt Jambi
    Platforms
    Android

    Default Re: QOpenGL QTimer, how do I animate this?

    How can the drawing methods in the "Hello-OpenGL" example be used for animation?
    a happy girl

Similar Threads

  1. <qopengl.h> library .lib?
    By giugio in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2013, 10:52
  2. QOpenGL in threads
    By wydesenej in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2013, 15:11
  3. Example Q, FLTKt and OpenGL without using QOpenGL
    By giorgik in forum Qt Programming
    Replies: 8
    Last Post: 27th December 2012, 18:56
  4. Replies: 15
    Last Post: 4th August 2012, 19:11
  5. how to use QTimer to animate QSlider
    By mike b in forum Newbie
    Replies: 1
    Last Post: 11th May 2010, 05:45

Tags for this Thread

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.