Monday 30 January 2012

What a game!!!

I've been playing around, pro-actively for a change, with Qt and coding for my phone: over the holidays I created a SDA hymnal app for my phone. I don't mean to brag but the Mrs and an aunt were both jealous enough to request that I port it to work on their Symbian phones. I plan to blog about my experience learning Qt maybe once every blue moon.

Actually I'll start right now and talk about event filters. There's code out there that serves as an example of how event filters work but I strongly doubt that the availability tutorials have reached a level of verbosity that can render my own take on the topic redundant. On the N900 (hildon/maemo 5), there are a few applications that have this nifty search functionality where once you slide open the keyboard and start typing the application starts to search for what you are typing. I wanted that functionality on the aforementioned hymnal. For this to work I first attempted to override the keyPressEvent method of the main window but found out that the QListView on the main window actually was stopping the key press event from getting to the main window. To handle scenarios like this on possible solution is to use event filters implemented by two functions - installEventFilter and eventFilter - that together allow a widget to pass responsibility for handling events to some other widget.

So with the QListView example above, let's assume the instance of the QListView is called listView and the instance of the QMainWindow on which the listView is displayed is called mainWindow. Then to redirect listView's events to mainWindow you have to call listView.installEventFilter(mainWindow). A way of understanding the installEventFilter function is that you've installed a siphon in the process by which listView usually handles it's events so that listView's events now slip out of its hands into mainWindow's. You then have to get mainWindow to handle the events that come its way from listView - this is where eventFilter method comes in. You have to override mainWindow's eventFilter method such that you do any event specific checks and target object checks.


I've included the code portion based on the example in the documentation here for for the fun of it. Now this is not my code o. I copied it almost verbatim from the Qt documentation but added some comments that may be useful if you read the epistle leading up to this paragraph

//within MainWindow's constructor set up the siphon filter
listView->installEventFilter(this); 

//then you override the eventFilter method
bool MainWindow::eventFilter(QObject *object, QEvent *event)
 {
     if (object == target && event->type() == QEvent::KeyPress) {
         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
         if (keyEvent->key() == Qt::Key_Tab) {
             // Special tab handling
             return true;
         } else
             return false;
     }
     return false;
 }



Now that you've read this you can now go on and read the Qt documentation here (http://developer.qt.nokia.com/doc/qt-4.8/eventsandfilters.html#event-filters) for actual code.