View Full Version : Eventfilter Problem
codeman
11th July 2012, 09:31
Hello friends,
I have a question. I have a 2 Qmainwindow. I put the one in the other as centralwidget to achieve the full height of the left and the right Dockwidget.I now have an outer QMainWindow and an inner QMainwindow. Then I add a buttomdockwidget with a textedit to inner QMainwindow. I would like to handle the event in the outer QMainwindow (
MainWindow::eventFilter(QObject *obj, QEvent *event)) and I intall it
this->mytextedit->installEventFilter(this); By this I mean the outer QMainwindow. So now when I hit a "s" for example I have an output like this "ss". It doubles my input.
Where is the misunderstanding of mine here?
Thanx in advance
wysota
11th July 2012, 12:21
What code did you put in the event filter?
codeman
11th July 2012, 12:32
//qDebug() << Q_FUNC_INFO << obj;
int keyInt(0);
Qt::Key key;
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
keyInt = keyEvent->key();
key = static_cast<Qt::Key>(keyInt);
if (event->type() == QEvent::KeyPress)
{
if(key == Qt::Key_unknown)
{
return true;
}
if( key == Qt::Key_Control ||
key == Qt::Key_Shift ||
key == Qt::Key_Alt ||
key == Qt::Key_Meta )
{
return true;
}
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
QString keyText = keyEvent->text();
qDebug() << "KEyTExt: " << keyText;
if(modifiers & Qt::ShiftModifier)
keyInt += Qt::SHIFT;
if(modifiers & Qt::ControlModifier)
keyInt += Qt::CTRL;
if(modifiers & Qt::AltModifier)
keyInt += Qt::ALT;
if(modifiers & Qt::MetaModifier)
keyInt += Qt::META;
QString objname = obj->objectName();
QString keySequence = QKeySequence(keyInt).toString(QKeySequence::Native Text);
if(this->_mapShortCutFunctor2.contains(objname) && this->_mapShortCutFunctor2[objname].contains(keySequence) )
{
(this->*_mapShortCutFunctor2[objname][keySequence])();
return true;
}
}
else
{
return QMainWindow::eventFilter(obj, event);
}
wysota
11th July 2012, 12:43
You definitely shouldn't assume you'll only be getting key events in the filter. This code might crash on line #5. What exactly are you trying to do? Why aren't you using QShortCut and QAction objects?
codeman
11th July 2012, 13:13
I try to collect all keypressed events or combinations of those in one place, where I can react depending on the object.
Have you a better solution. I am learning by doing. Doing it wrong perhaps helps also to find out the right way;o)).
I am open for any suggestions.
wysota
11th July 2012, 13:17
The proper way would be to use a bunch of QAction instances (alternatively QShortcut instances here and there) and enable/disable them depending on the context.
codeman
11th July 2012, 14:40
Ok thank you
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.