source code (from help of Qt4.6) is
protected:
MainWindow::MainWindow()
{
ui->menuBar->installEventFilter(this);
}
{
if (obj == ui->menuBar) {
if (event
->type
() == QEvent::MouseButtonPress) { QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
qDebug() << "key pressed" << keyEvent->key();
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
}
}
protected:
bool eventFilter(QObject *obj, QEvent *ev);
MainWindow::MainWindow()
{
ui->menuBar->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->menuBar) {
if (event->type() == QEvent::MouseButtonPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
qDebug() << "key pressed" << keyEvent->key();
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
}
To copy to clipboard, switch view to plain text mode
but for each time of click on the same menubar item, i receive a new number or receive the same number for different menubar items:
Ate key press 9
Ate key press 7
Ate key press 7
Ate key press 8
Ate key press 8
Ate key press 11
Ate key press 10
Ate key press 9
Ate key press 9
Ate key press 10
Ate key press 10
Ate key press 10
Ate key press 10
Ate key press 10
Ate key press 10
Ate key press 9
Ate key press 9
Ate key press 9
Ate key press 9
Ate key press 6
Ate key press 6
Ate key press 6
what is the solution?
Bookmarks