From 7824a9ba3145ea7c2184ea67ae8969d178c53065 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Sun, 28 Sep 2014 18:58:46 +0200 Subject: Make qopenglwidget example functional without timers too Change-Id: I6a89eaf794202c45a5ad3152d304e46041704730 Reviewed-by: Gunnar Sletta --- examples/opengl/qopenglwidget/glwidget.cpp | 5 +++++ examples/opengl/qopenglwidget/mainwindow.cpp | 25 ++++++++++++++++++++++++- examples/opengl/qopenglwidget/mainwindow.h | 5 +++++ 3 files changed, 34 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp index de581f8d47..d39e1992a7 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 7645c75d8c..a3c8d5edcf 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 9db3e8cbec..353586c42c 100644 --- a/examples/opengl/qopenglwidget/mainwindow.h +++ b/examples/opengl/qopenglwidget/mainwindow.h @@ -45,6 +45,8 @@ #include #include +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 m_glWidgets; }; #endif -- cgit v1.2.3