PDA

View Full Version : Eventfilter problem



codeman
3rd July 2012, 09:48
Hello friends,

I have a subclassed qtextedit in a QMAinwindow. So my target is to achieve that the key events from textedit
are handled in Mainwindow event system. so when I make this:


//************************************************** ************************
class keyPressCatcher:public QObject
//************************************************** ************************
{
public:
keyPressCatcher():QObject()
{};
~keyPressCatcher()
{};

bool eventFilter(QObject* object,QEvent* event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);

std::cout << "You Pressed " << keyEvent->text().toStdString() << "\n";
return true;
}
else
{
// standard event processing
return QObject::eventFilter(object, event);
}



};


};

and install the eventfilter


this->installEventFilter(new keyPressCatcher());

and textedit->installEventFilter(this);

nothing appears.

So my question is what the the basic step to achieve this functionality?

Thanx in advance

high_flyer
3rd July 2012, 10:50
So my target is to achieve that the key events from textedit
are handled in Mainwindow event system.
You are confusing things.
You don't need the keyPressCatcher class.
Just overload eventFilter() in your main window class and then you only need to install the main window as event filter:

textedit->installEventFilter(this);

codeman
3rd July 2012, 12:58
Ok, I implement it as you suggest. Now when I hit Ctrl+E or whatever alpabetically concunjction I run into

MainWindow::keyPressEvent(QKeyEvent* event)

but when I hit Enter in TExtedit I have no reaction.

high_flyer
3rd July 2012, 13:30
Ok, I implement it as you suggest. Now when I hit Ctrl+E or whatever alpabetically concunjction I run into
How do you know you run into the key handler?
Is because your code is doing something or by setting a break point?
If the former, you are probably not using the correct key code.
Note that there is Qt::Key_Return and Qt::Key_Enter.

codeman
3rd July 2012, 13:45
Here are the event method:


//************************************************** ************************
void MainWindow::keyPressEvent(QKeyEvent* event)
//************************************************** ************************
{
qDebug() << Q_FUNC_INFO;
Qt::Key key;
int keyInt(0);
QKeyEvent *keyEvent;
if (event->type() == QEvent::KeyPress)
{
keyEvent = static_cast<QKeyEvent*>(event);
keyInt = keyEvent->key();
key = static_cast<Qt::Key>(keyInt);
if(key == Qt::Key_unknown){
qDebug() << "Unknown key from a macro probably";
return;
}
}
// the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if(key == Qt::Key_Control ||
key == Qt::Key_Shift ||
key == Qt::Key_Alt ||
key == Qt::Key_Meta ||
key == Qt::Key_Return ||
key == Qt::Key_Enter )
{
qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta or Enter/Return";
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::Native Text);
return;
}
// check for a combination of user clicks
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
QString keyText = keyEvent->text();
// if the keyText is empty than it's a special key like F1, F5, ...
qDebug() << "Pressed Key:" << 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;

qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::Native Text);

}

high_flyer
3rd July 2012, 13:49
and for enter you get nothing?
What platform are you on?

codeman
3rd July 2012, 14:01
Yes, no qDebug() output: I am on Win7 64Bit/ Qt 4.8.1 for Desktop - MSVC2010

high_flyer
3rd July 2012, 14:02
Yes, no qDebug() output:
Oh, so that's ok then.
What is the problem then?

codeman
3rd July 2012, 14:09
When I hit Enter in TExtedit I would like to enter this section in KeypressEvent:


if(key == Qt::Key_Control ||
key == Qt::Key_Shift ||
key == Qt::Key_Alt ||
key == Qt::Key_Meta ||
key == Qt::Key_Return ||
key == Qt::Key_Enter )
{
qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta or Enter/Return";
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::Native Text);
return;
}

But I have no output.

high_flyer
3rd July 2012, 14:15
But your event filter method is being called - so just step through and see where the code is going and why - you have to debug - its pars of software development.
See what key you actually get, and that the code runs the way you actually meant.

codeman
3rd July 2012, 14:21
Thats the problem. When I hit Ctrl+d or F1 or only Ctrl I run into my
MainWindow::keyPressEvent(QKeyEvent* event)
But when I only press Enter I get a carriage return in my textedit but no event is triggered so I cant debug.

So when I reimplement my Textedit::keyPressEvent

and call
if(e->key() == Qt::Key_Return)
qDebug() << "Hallo";

I get my output;

I provide an example project where you can see it if you can surely(when you have the time & fun). When I handle return in MAinwindow no event is triggered. When I reimplement it again in Textedit I can catch the event.
I cannot add a zip archiv....

high_flyer
3rd July 2012, 15:29
But when I only press Enter I get a carriage return in my textedit but no event is triggered so I cant debug.
Oh, I read to fast your previous answer, I thought you DO get a qDebug().

But you can debug - set a break point in the first line of the function - that is the only sure way to know.

In the information you provided I can't see anything else.
Post you project, and i will try it.

codeman
3rd July 2012, 15:43
So here is the project:
7930
By the way the breakpoint in my event method and hitting Enter does not enter this method.

Thanx in advance

high_flyer
3rd July 2012, 15:56
MainWindow::keyPressEvent()
should be
MainWindow::eventFilter()

codeman
3rd July 2012, 16:10
And what is inside the bracket of your event method

high_flyer
3rd July 2012, 16:13
Sorry I don't follow....?
http://qt-project.org/doc/qt-4.8/qobject.html#eventFilter

codeman
3rd July 2012, 16:15
Is this correct:

MainWindow::eventFilter(QKeyEvent* event)
MainWindow::eventFilter(QKeyEvent* event)

high_flyer
3rd July 2012, 16:18
Please read the eventFilter docs.
Don't expect to be fed every little detail.
http://qt-project.org/doc/qt-4.8/qobject.html#eventFilter

codeman
3rd July 2012, 17:21
Ok thank you very much