Results 1 to 2 of 2

Thread: Replace keyPressEvent (dot with tab)

  1. #1
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question Replace keyPressEvent (dot with tab)

    I have to get an IP address so I have four QLineEdit input box like this:

    Qt Code:
    1. [ ] . [ ] . [ ] . [ ]
    To copy to clipboard, switch view to plain text mode 

    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):
    Qt Code:
    1. [...]
    2. private:
    3. bool eventFilter ( QObject * , QEvent * );
    4. [...]
    To copy to clipboard, switch view to plain text mode 

    (dialog object c++ code):
    Qt Code:
    1. [...]
    2. mylineEditGetAddressIP_A->installEventFilter(this);
    3. [...]
    4. bool dialogGetAddressObject::eventFilter ( QObject *object , QEvent *event )
    5. {
    6. if (event->type() == QEvent::KeyPress)
    7. {
    8. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    9. if ( keyEvent->key() == Qt::Key_Period )
    10. {
    11. qDebug () << "Event: Qt::Key_Period - Insert Qt::Key_Tab event...";
    12. QKeyEvent * myKeyEvent = new QKeyEvent ( QEvent::KeyPress , Qt::Key_Tab , Qt::NoModifier, 0);
    13. QWidget::keyPressEvent ( myKeyEvent );
    14. return true;
    15. }
    16. }
    17. return false;
    18. }
    19. [...]
    To copy to clipboard, switch view to plain text mode 

    where is my mistake?
    thanks,
    the_bis

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Replace keyPressEvent (dot with tab)

    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().
    Qt Code:
    1. if ( keyEvent->key() == Qt::Key_Period )
    2. {
    3. qDebug () << "Event: Qt::Key_Period - Insert Qt::Key_Tab event...";
    4. focusNextChild();
    5. return true;
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Replies: 6
    Last Post: 13th May 2008, 13:19
  2. QTreeWidget keyPressEvent
    By hgedek in forum Qt Programming
    Replies: 3
    Last Post: 18th August 2007, 16:33
  3. F2 keyPressEvent Not Capturing
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 2nd August 2007, 20:41
  4. Handling of dead keys in keyPressEvent()
    By ghorwin in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2006, 12:26
  5. KeyPressEvent behaves strange, skips the F key.
    By pir in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2006, 17:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.