Saturday, 8 December 2018

Get the keyPress-Event in QGis-Plugin


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?



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...