Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

    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?

share|improve this question
up vote 0 down vote accepted

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

     class KeyPressEater : public QObject

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


but in your case it would be

     KeyPressEater *keyPressEater = new KeyPressEater(this);
     QTreeWidget *tree= new QTreeWidget(this);
share|improve this answer
Thank you a lot! The Code works and I'am happy. – schmiddor Dec 19 '12 at 10:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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