diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp index de581f8d470eddbc99ca2f8954fc1bd1f2e05c6b..d39e1992a7d68852052a73b9d8048d3de10c281f 100644 --- a/examples/opengl/qopenglwidget/glwidget.cpp +++ b/examples/opengl/qopenglwidget/glwidget.cpp @@ -394,6 +394,11 @@ void GLWidget::paintGL() } m_fAngle += 1.0f; ++m_frames; + + // When requested, follow the ideal way to animate: Rely on + // blocking swap and just schedule updates continuously. + if (!m_mainWindow->timerEnabled()) + update(); } void GLWidget::createBubbles(int number) diff --git a/examples/opengl/qopenglwidget/mainwindow.cpp b/examples/opengl/qopenglwidget/mainwindow.cpp index 7645c75d8cde5656af84ec15954d9ca61e5f9d23..a3c8d5edcff6009300860a6e33df1270e4d347fe 100644 --- a/examples/opengl/qopenglwidget/mainwindow.cpp +++ b/examples/opengl/qopenglwidget/mainwindow.cpp @@ -54,6 +54,7 @@ MainWindow::MainWindow() : m_nextX(1), m_nextY(1) { GLWidget *glwidget = new GLWidget(this, true, qRgb(20, 20, 50)); + m_glWidgets << glwidget; QLabel *label = new QLabel(this); m_timer = new QTimer(this); QSlider *slider = new QSlider(this); @@ -67,12 +68,19 @@ MainWindow::MainWindow() "Note that on most systems the swap will block to wait for vsync\n" "and therefore an interval < 16 ms will likely lead to a 60 FPS update rate."); QGroupBox *updateGroupBox = new QGroupBox(this); + QCheckBox *timerBased = new QCheckBox("Use timer", this); + timerBased->setChecked(true); + timerBased->setToolTip("Toggles using a timer to trigger update().\n" + "When not set, each paintGL() schedules the next update immediately,\n" + "expecting the blocking swap to throttle the thread.\n" + "This shows how unnecessary the timer is in most cases."); QCheckBox *transparent = new QCheckBox("Transparent background", this); transparent->setToolTip("Toggles Qt::WA_AlwaysStackOnTop and transparent clear color for glClear().\n" "Note how the button on top stacks incorrectly when enabling this."); QHBoxLayout *updateLayout = new QHBoxLayout; updateLayout->addWidget(updateLabel); updateLayout->addWidget(updateInterval); + updateLayout->addWidget(timerBased); updateLayout->addWidget(transparent); updateGroupBox->setLayout(updateLayout); @@ -123,6 +131,8 @@ MainWindow::MainWindow() connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent); connect(updateInterval, SIGNAL(valueChanged(int)), this, SLOT(updateIntervalChanged(int))); + connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged); + connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled); m_timer->start(); } @@ -130,7 +140,8 @@ MainWindow::MainWindow() void MainWindow::updateIntervalChanged(int value) { m_timer->setInterval(value); - m_timer->start(); + if (m_timer->isActive()) + m_timer->start(); } void MainWindow::addNew() @@ -138,6 +149,7 @@ void MainWindow::addNew() if (m_nextY == 4) return; GLWidget *w = new GLWidget(this, false, qRgb(qrand() % 256, qrand() % 256, qrand() % 256)); + m_glWidgets << w; connect(m_timer, SIGNAL(timeout()), w, SLOT(update())); m_layout->addWidget(w, m_nextY, m_nextX, 1, 1); if (m_nextX == 3) { @@ -147,3 +159,14 @@ void MainWindow::addNew() ++m_nextX; } } + +void MainWindow::timerUsageChanged(bool enabled) +{ + if (enabled) { + m_timer->start(); + } else { + m_timer->stop(); + foreach (QOpenGLWidget *w, m_glWidgets) + w->update(); + } +} diff --git a/examples/opengl/qopenglwidget/mainwindow.h b/examples/opengl/qopenglwidget/mainwindow.h index 9db3e8cbecc2ccaff947c855c1ef926a7b2110ac..353586c42cbbf83a89834796faab56df82f3f825 100644 --- a/examples/opengl/qopenglwidget/mainwindow.h +++ b/examples/opengl/qopenglwidget/mainwindow.h @@ -45,6 +45,8 @@ #include <QTimer> #include <QGridLayout> +QT_FORWARD_DECLARE_CLASS(QOpenGLWidget) + class MainWindow : public QMainWindow { Q_OBJECT @@ -52,15 +54,18 @@ class MainWindow : public QMainWindow public: MainWindow(); void addNew(); + bool timerEnabled() const { return m_timer->isActive(); } private slots: void updateIntervalChanged(int value); + void timerUsageChanged(bool enabled); private: QTimer *m_timer; QGridLayout *m_layout; int m_nextX; int m_nextY; + QVector<QOpenGLWidget *> m_glWidgets; }; #endif