PDA

View Full Version : Replace keyPressEvent (dot with tab)



the_bis
5th June 2008, 17:51
I have to get an IP address so I have four QLineEdit input box like this:


[ ] . [ ] . [ ] . [ ]

I can insert the first number in hte first QLineEdit (mylineEditGetAddressIP_A) and I can go to the second by pressing the tab key. My goal is to skip to the next lineedit also when I press the "." key.

This is my not working code:

(dialog object header):

[...]
private:
bool eventFilter ( QObject * , QEvent * );
[...]

(dialog object c++ code):


[...]
mylineEditGetAddressIP_A->installEventFilter(this);
[...]
bool dialogGetAddressObject::eventFilter ( QObject *object , QEvent *event )
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ( keyEvent->key() == Qt::Key_Period )
{
qDebug () << "Event: Qt::Key_Period - Insert Qt::Key_Tab event...";
QKeyEvent * myKeyEvent = new QKeyEvent ( QEvent::KeyPress , Qt::Key_Tab , Qt::NoModifier, 0);
QWidget::keyPressEvent ( myKeyEvent );
return true;
}
}
return false;
}
[...]

where is my mistake?
thanks,
the_bis

jpn
6th June 2008, 10:43
First of all, the event is sent to a wrong widget (it should be sent to the line edit, not to the window). Just notice that sending an event to the receiver inside an event filter causes an infinite loop. Secondly, you've got a memory leak there (the event is never deleted). Thirdly, there is QWidget::focusNextChild(). ;)


if ( keyEvent->key() == Qt::Key_Period )
{
qDebug () << "Event: Qt::Key_Period - Insert Qt::Key_Tab event...";
focusNextChild();
return true;
}