1

I have a QTreeWidget in a QWidget which is the gui of my plugin(cpp). Now i want to get the keyPress-Event of a Node in the TreeView. I try to reimplement the signal of the QWidget.

protected:
    virtual void keyPressEvent(QKeyEvent *);

But this don't work. I think the problem is: My gui class derives from QWidget(the signal will be reimplemented), but it has also added the QTreeWidget as member, and the signal from it should be reimplemented? But how to do this? Any suggestions?

1 Answer 1

0

You will not be able to override this method unless you subclass QTreeWidget and override the method. However what you can do is use an event filter:

Here is an example taken from http://doc.qt.digia.com/4.4/qobject.html#installEventFilter

     class KeyPressEater : public QObject
     {
         Q_OBJECT
         ...

     protected:
         bool eventFilter(QObject *obj, QEvent *event);
     };

     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);
         }
     }

To install

     KeyPressEater *keyPressEater = new KeyPressEater(this);
     QPushButton *pushButton = new QPushButton(this);
     QListView *listView = new QListView(this);

     pushButton->installEventFilter(keyPressEater);
     listView->installEventFilter(keyPressEater);

but in your case it would be

     KeyPressEater *keyPressEater = new KeyPressEater(this);
     QTreeWidget *tree= new QTreeWidget(this);
     tree->installEventFilter(keyPressEater);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.