Make qopenglwidget example functional without timers too

Change-Id: I6a89eaf794202c45a5ad3152d304e46041704730
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
This commit is contained in:
Laszlo Agocs 2014-09-28 18:58:46 +02:00
parent 90ce9701d0
commit 7824a9ba31
3 changed files with 34 additions and 1 deletions

View File

@ -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)

View File

@ -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();
}
}

View File

@ -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