ui: Added timer stopper to QtGraphicsView to stop movement if keyup event not registered

This commit is contained in:
Eberhard Graether
2016-04-19 23:13:41 +02:00
parent f19cdc3bb9
commit ce5c3e8626
2 changed files with 16 additions and 1 deletions
+14 -1
View File
@@ -18,6 +18,10 @@ QtGraphicsView::QtGraphicsView(QWidget* parent)
m_timer = std::make_shared<QTimer>(this);
connect(m_timer.get(), SIGNAL(timeout()), this, SLOT(update()));
m_timerStopper = std::make_shared<QTimer>(this);
m_timerStopper->setSingleShot(true);
connect(m_timerStopper.get(), SIGNAL(timeout()), this, SLOT(stopTimer()));
}
float QtGraphicsView::getZoomFactor() const
@@ -54,6 +58,8 @@ void QtGraphicsView::mouseReleaseEvent(QMouseEvent *event)
void QtGraphicsView::keyPressEvent(QKeyEvent* event)
{
bool moved = moves();
switch (event->key())
{
case Qt::Key_W:
@@ -80,10 +86,12 @@ void QtGraphicsView::keyPressEvent(QKeyEvent* event)
return;
}
if (moves())
if (!moved && moves())
{
m_timer->start(20);
}
m_timerStopper->start(1000);
}
void QtGraphicsView::keyReleaseEvent(QKeyEvent* event)
@@ -183,6 +191,11 @@ void QtGraphicsView::update()
}
}
void QtGraphicsView::stopTimer()
{
m_timer->stop();
}
bool QtGraphicsView::moves() const
{
return m_up || m_down || m_left || m_right;
+2
View File
@@ -34,6 +34,7 @@ signals:
private slots:
void update();
void stopTimer();
private:
bool moves() const;
@@ -53,6 +54,7 @@ private:
bool m_shift;
std::shared_ptr<QTimer> m_timer;
std::shared_ptr<QTimer> m_timerStopper;
};
#endif // QT_GRAPHICS_VIEW_H