Input Handling in Qt
-
Qt is great because it already has classes such as QKeyPressEvent and QMouseWheel event; however coming over from SDL it's rather difficult to know how I can manage input more centrally on Qt. QOpenGLWidget of type QWidget already has inherited functions that manage input handling; but because I'm developing an app with OpenGL obviously I need a more central way of handling input rather than putting the code all over the place. Under SDL, I had an InputHandler class that managed everything I needed. Now, if I were to develop an InputHandler class, how could I integrate Qt in it? The InputHandler class shouldn't really inherit from QKeyEvent etc, but rather favour encapsulation instead such that there's an interface which makes it easier to use.
What are your solutions here? Is an InputHandler class something really required, given that it would utilise event-based programming; or would encapsulating the event classes outside be more feasible? Personally, I prefer the former. Thus, in a class that needs handles input I might do:
@class inputHandling
{
public:
inputHandling();
~inputHandling();bool isKeyPressed(Qt::Key code);
private:
QKeyPressEvent *keyEvent;
QMouseWheel *mWheelEvent;
}@Would this class, using encapsulation, be a practical approach in handling input within Qt? Thanks for reading!
-
One way is to install your inputHandling instance as an event filter. You can install that on QApplication if you want. Then, for every event, the function eventFilter((QObject * watched, QEvent * event) will be called on that object. You can decide how to handle that then.
-
[quote author="Andre" date="1422977247"]One way is to install your inputHandling instance as an event filter. You can install that on QApplication if you want. Then, for every event, the function eventFilter((QObject * watched, QEvent * event) will be called on that object. You can decide how to handle that then.
[/quote]
I don't really follow. How would I call this eventFitler function? InputHandler isn't a QObject, and I'd probably have to instantiate an event. Would this happen within the class that inherits from QOpenGLWidget?
Btw, I've also implemented these functions for input handling:
@void InputHandler::listen(QEvent event)
{
pKeyEvent = static_cast<QKeyEvent>(event);
pMouseEvent = static_cast<QMouseEvent*>(event);
pWheelEvent = static_cast<QWheelEvent*>(event);if (event->type() == QEvent::KeyRelease) pKeyPressed = true; if (event->type() == QEvent::MouseButtonPress) pMousePressed = true; if (event->type() == QEvent::Wheel) pWheelRolled = true; pIsListening = true;
}@
And then for a function like isKeyPressed:
@bool InputHandler::isKeyPressed(Qt::Key code) const
{
if (pKeyPressed)
{
if (pKeyEvent->key() == code)
return true;
}
}@The thing is, these functions need to be called within a function that is constantly updating. Thus, paintGL() seems to suffice for now. My proposal is that I should instantiate QEvent within glWidget and pass that in. I only need to know if it actually can pick up events or whether a bit more work is needed. At hindsight, it probably requires that eventFilter.