Results 1 to 11 of 11

Thread: QOpenGL QTimer, how do I animate this?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Dec 2024
    Posts
    1
    Qt products
    Qt/Embedded
    Platforms
    Symbian S60

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

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QTimer>
    4. #include <QPainter>
    5. #include <QVector>
    6. #include <QPointF>
    7. #include <cmath>
    8.  
    9. class Triangle {
    10. public:
    11. QPointF position;
    12. double angle; // Rotation angle
    13. double scale; // Scale factor
    14.  
    15. Triangle(QPointF pos) : position(pos), angle(0), scale(1) {}
    16.  
    17. void update() {
    18. // Update angle and scale for animation
    19. angle += 1; // Rotate by 1 degree
    20. scale = 1 + 0.5 * sin(M_PI * angle / 180); // Scale between 1 and 1.5
    21. }
    22. };
    23.  
    24. class TriangleWidget : public QWidget {
    25. QVector<Triangle> triangles;
    26. QTimer timer;
    27.  
    28. public:
    29. TriangleWidget(QWidget *parent = nullptr) : QWidget(parent) {
    30. // Initialize triangles
    31. for (int i = 0; i < 10; ++i) {
    32. triangles.append(Triangle(QPointF(100 + i * 50, 100)));
    33. }
    34.  
    35. // Set up the timer
    36. connect(&timer, &QTimer::timeout, this, &TriangleWidget::updateAnimation);
    37. timer.start(16); // Approximately 60 FPS
    38. }
    39.  
    40. protected:
    41. void paintEvent(QPaintEvent *) override {
    42. QPainter painter(this);
    43. painter.setRenderHint(QPainter::Antialiasing);
    44.  
    45. for (const Triangle &triangle : triangles) {
    46. painter.save();
    47. painter.translate(triangle.position);
    48. painter.rotate(triangle.angle);
    49. painter.scale(triangle.scale, triangle.scale);
    50. painter.drawPolygon(QPolygonF() << QPointF(0, -20) << QPointF(-20, 20) << QPointF(20, 20));
    51. painter.restore();
    52. }
    53. }
    54.  
    55. private:
    56. void updateAnimation() {
    57. for (Triangle &triangle : triangles) {
    58. triangle.update();
    59. }
    60. update(); // Trigger a repaint
    61. }
    62. };
    63.  
    64. int main(int argc, char *argv[]) {
    65. QApplication app(argc, argv);
    66. TriangleWidget window;
    67. window.resize(800, 600);
    68. window.show();
    69. return app.exec();
    70. }
    To copy to clipboard, switch view to plain text mode 

    Moderator edit: Do not post spam links disguised in code. No more warnings. Next time, you'll be banned and your posts removed.
    Last edited by d_stranz; 28th December 2024 at 00:12. Reason: missing [code] tags

Similar Threads

  1. <qopengl.h> library .lib?
    By giugio in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2013, 11:52
  2. QOpenGL in threads
    By wydesenej in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2013, 16:11
  3. Example Q, FLTKt and OpenGL without using QOpenGL
    By giorgik in forum Qt Programming
    Replies: 8
    Last Post: 27th December 2012, 19:56
  4. Replies: 15
    Last Post: 4th August 2012, 20:11
  5. how to use QTimer to animate QSlider
    By mike b in forum Newbie
    Replies: 1
    Last Post: 11th May 2010, 06: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
  •  
Qt is a trademark of The Qt Company.