I know I need an event filter for Qt 4 to handle keyboard events. But I previously had something similar working without a need to manually configure this. I’ve read the documentation for it, but I was unable to get an event eater setup for the application. I don’t need events managed at the button level.
Here is example code that illustrates two buttons that will not focus or activate except by mouse click. I would like one to have focus by default on one button, which I can do with button.setFocus(). I would also like to be able to navigate buttons which are active by using tab, or up/down keys then activate with enter/space. The problem is I’m confused on how to implement it. Do I need to inherit some Q* class and implement an event filter class, then install on objects? Or is there a more simple way?
#include <QApplication>
#include <QPushButton>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
QPushButton quit("Quit", &window);
quit.setGeometry(10, 40, 180, 40);
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
QPushButton nothing("Do Nothing", &window);
nothing.setGeometry(10, 80, 180, 40);
quit.setFocus();
window.show();
return app.exec();
}
You don’t have to do anything to be able to navigate between buttons using tab or cursor keys, this is the default behavior.