Reputation: 1582
Currently I want to handle basic key handling; e.g. if key is pressed, do this. However, should this have its own class or be done within a single function? Qt has a means of recording key events, but I know not how to proceed. I could have:
void keyPrHandle(QKeyEvent *Ev)
{
if (Ev->key() == Qt::Key_G)
ui->label->setText(Ev->text());
}
Else I would have to build a class that handles key events. I don't know how Qt handles key events, nor how I should implement it.
Upvotes: 1
Views: 931
Reputation: 3854
Every QWidget
handles key events. Where you should implement the key handling depends on where you do need these. If your whole program should act on key presses you should overload keyPressEvent()
in your QMainWindow
.
Of course, if child widgets are active (like form input widgets), those take up the key handling. This might mean you have to intercept those events as well.
Upvotes: 1
Reputation: 18524
You can do this in derived class. Suppose you have QBase and QDerive classes. Then you can reimplement event handlers:
void QDerived::keyPressEvent(QKeyEvent *Ev)
{
if (Ev->key() == Qt::Key_G)
//do something
QBase::keyPressEvent(Ev);
}
And also in QDerive class:
protected:
void keyPressEvent(QKeyEvent *Ev);
You can also handle events without subclassing with event filters.
Upvotes: 1