Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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 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);
share|improve this answer
    
Thank you a lot! The Code works and I'am happy. –  schmiddor Dec 19 '12 at 10:38

Your Answer

 
discard

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.