0

I have a MainWindow with a QLabel for visualize some QPixmap. I have subclassed the QLabel class in, let's say, MyVisualizer, for handling the QPixmap generation/modification/whatever.

Now in the MainWindow, with a key pressed I want the QLabel to fullscreen, and due to the showFullScreen method works only on windows, I have created a QDialog, moved the myvisualizer instance inside of it, and called QDialog::showFullScreen.Then, I'd like to have another key-pressed listener in the QDialog for some other actions.

Is it possible to add a keyPressEvent(QKeyEvent *e) callback to QDialog without subclassing it?

4

2 回答 2

4

是的,您可以安装 eventFilter。

void QObject::installEventFilter(QObject * filterObj)
于 2013-08-13T11:29:23.857 回答
0
 class KeyPressEater : public QDialog
 {
     Q_OBJECT
     ...
     KeyPressEater(Qobject paarent);
 protected:
     bool eventFilter(QObject *obj, QEvent *event);
 };
 KeyPressEater::KeyPressEater(Qobject* parent) : QDialog(parent)
{
  installEventFilter(this);
}
 bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
 {
     if (event->type() == QEvent::KeyPress) {
         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
         qDebug("Ate key press %d", keyEvent->key());
         return true;
     } else {
         // standard event processing
         return QObject::eventFilter(obj, event);
     }
 }
于 2013-08-13T11:30:15.320 回答