Results 1 to 15 of 15

Thread: Posting a QKeyEvent to a QLineEdit

  1. #1
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Posting a QKeyEvent to a QLineEdit

    I am trying to post a QKeyEvent to a QLineEdit. The line edit seems to receive the event (I have tested this with an event filter), but the character is not displayed in the QLineEdit. Does anyone know why the QLineEdit does not display the character? Here is the code:

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "TestForm.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6.  
    7. QApplication app(argc, argv);
    8.  
    9. TestForm form;
    10. form.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    TestForm.h
    Qt Code:
    1. #ifndef TEST_FORM_H
    2. #define TEST_FORM_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QLineEdit;
    7.  
    8. class TestForm : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. TestForm(QDialog* parent = 0);
    14.  
    15. public slots:
    16. void postKeyEvent();
    17.  
    18. private:
    19. QLineEdit* _line_edit;
    20. };
    21.  
    22. #endif // TEST_FORM_H
    To copy to clipboard, switch view to plain text mode 

    TestForm.h
    Qt Code:
    1. #include <QDialog>
    2. #include <QFrame>
    3. #include <QHBoxLayout>
    4. #include <QPushButton>
    5. #include <QLineEdit>
    6. #include <QSpacerItem>
    7. #include <QVBoxLayout>
    8. #include <QKeyEvent>
    9. #include <QApplication>
    10.  
    11.  
    12. #include "TestForm.h"
    13.  
    14.  
    15. TestForm::TestForm(QDialog* parent)
    16. : QDialog(parent)
    17. {
    18. QFrame* frame = new QFrame(this);
    19. frame->setFrameShape(QFrame::StyledPanel);
    20. frame->setFrameShadow(QFrame::Raised);
    21.  
    22. QHBoxLayout* hboxLayout_2 = new QHBoxLayout(frame);
    23. hboxLayout_2->setSpacing(6);
    24. hboxLayout_2->setMargin(0);
    25.  
    26. _line_edit = new QLineEdit(frame);
    27.  
    28. hboxLayout_2->addWidget(_line_edit);
    29.  
    30. QPushButton* button = new QPushButton(frame);
    31. button->setText("Post key event");
    32.  
    33. hboxLayout_2->addWidget(button);
    34.  
    35. QObject::connect(button, SIGNAL(clicked()), this, SLOT(postKeyEvent()));
    36.  
    37.  
    38. QHBoxLayout* hboxLayout = new QHBoxLayout();
    39. hboxLayout->setSpacing(6);
    40. hboxLayout->setMargin(0);
    41.  
    42. QSpacerItem* spacerItem = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum);
    43.  
    44. hboxLayout->addItem(spacerItem);
    45.  
    46. QPushButton* okButton = new QPushButton(this);
    47. okButton->setText("OK");
    48.  
    49. hboxLayout->addWidget(okButton);
    50.  
    51. QPushButton* cancelButton = new QPushButton(this);
    52. cancelButton->setText("Cancel");
    53.  
    54. hboxLayout->addWidget(cancelButton);
    55.  
    56. QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    57. vboxLayout->setSpacing(6);
    58. vboxLayout->setMargin(9);
    59.  
    60. vboxLayout->addWidget(frame);
    61.  
    62. vboxLayout->addLayout(hboxLayout);
    63.  
    64. // Connect the button clicked signals to the form slots
    65. QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    66. QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    67.  
    68. }
    69.  
    70.  
    71.  
    72. void TestForm::postKeyEvent()
    73. {
    74.  
    75. QApplication::postEvent(_line_edit,
    76. new QKeyEvent(QEvent::KeyPress,
    77. Qt::Key_A,
    78. Qt::NoModifier));
    79. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by cocheci; 30th May 2006 at 21:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Posting a QKeyEvent to a QLineEdit

    What about this?
    Qt Code:
    1. void TestForm::postKeyEvent()
    2. {
    3. QApplication::postEvent(_line_edit,
    4. new QKeyEvent(QEvent::KeyPress,
    5. Qt::Key_A,
    6. Qt::NoModifier));
    7. QApplication::postEvent(_line_edit,
    8. new QKeyEvent(QEvent::KeyRelease,
    9. Qt::Key_A,
    10. Qt::NoModifier));
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posting a QKeyEvent to a QLineEdit

    I had tried that, and that doesn't work either. Additionally the QLineEdit reacts to the press event when the event is sent by the system (i.e. when you press the keyboard character key), it displays the character immediately, before the release event.

    Thanks,
    Cristian

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

    Default Re: Posting a QKeyEvent to a QLineEdit

    QLineEdit uses QKeyEvent::text() for inserting text.
    Qt Code:
    1. QApplication::postEvent(_line_edit,
    2. new QKeyEvent(QEvent::KeyPress,
    3. Qt::Key_A,
    4. Qt::NoModifier,
    5. "a")); // <-QKeyEvent::text
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    cocheci (31st May 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Posting a QKeyEvent to a QLineEdit

    Can't you simply use QLineEdit::setText() or QLineEdit::insert()?

  7. #6
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posting a QKeyEvent to a QLineEdit

    Quote Originally Posted by wysota
    Can't you simply use QLineEdit::setText() or QLineEdit::insert()?
    I could have, but I was just wondering why the posting of the key press event does not work.
    My next question is why posting a QMousePress event to the QLineEdit doesn't do the same thing as it does when run interactively (i.e. set the keyboard focus on the QLineEdit).

    Thanks,
    Cristian

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Posting a QKeyEvent to a QLineEdit

    Because it is QFocusEvent's responsibility.

  9. The following user says thank you to wysota for this useful post:

    cocheci (2nd June 2006)

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

    Default Re: Posting a QKeyEvent to a QLineEdit

    It's the OS which is responsible for sending appropriate events. There are separate mouse and focus in/out events. A single mouse event doesn't cause all other appropriate events to be sent (compared to the situation what happens upon a mouse click).

    QWidget offers methods for setting focus and QLineEdit (or QTextCursor) offers methods for setting cursor position. Don't try to re-invent a wheel..
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    cocheci (2nd June 2006)

  12. #9
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posting a QKeyEvent to a QLineEdit

    Quote Originally Posted by jpn
    It's the OS which is responsible for sending appropriate events. There are separate mouse and focus in/out events. A single mouse event doesn't cause all other appropriate events to be sent (compared to the situation what happens upon a mouse click).

    QWidget offers methods for setting focus and QLineEdit (or QTextCursor) offers methods for setting cursor position. Don't try to re-invent a wheel..
    Quote Originally Posted by wysota
    Because it is QFocusEvent's responsibility.

    But ... here's what I'm trying to achieve: I am trying to write automatic unit tests for my widgets by "simulating" the user interaction with the widget. E.g. for a left mouse press I am sending a MousePress event to the widget. Are you saying that's not enough? Are you saying that I cannot automate the simulation of the user interaction with the widget?

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Posting a QKeyEvent to a QLineEdit

    Quote Originally Posted by cocheci
    Are you saying that I cannot automate the simulation of the user interaction with the widget?
    You can, but you have to send all events that normally would be sent to that widget. You could use event filter to record all events and then just replay them.

  14. #11
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posting a QKeyEvent to a QLineEdit

    Quote Originally Posted by jacek
    You can, but you have to send all events that normally would be sent to that widget. You could use event filter to record all events and then just replay them.
    I tried that, but it didn't quite work as in interactive mode. The LineEdit receives 4 events when the user clicks on it:

    Enter
    FocusIn
    MousePress
    MouseRelease

    When I send these four events to the line edit, the bliking cursor is displayed (in the LineEdit), but the LineEdit never receives the keyboard focus. I even sent a FocusOut event to the widget that is supposed to lose focus, but the LineEdit still didn't gain focus.

    Thanks,
    Cristian

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

    Default Re: Posting a QKeyEvent to a QLineEdit

    Looks like it might be impossible to handle it this way. Still if you send a focus event and the widget even receives it, QApplication won't be tricked. Although the widget prepares itself as if it had been focused (like QLineEdit's cursor starts blinking etc.), the next (real) key press event still goes to a widget which the qApp believes that has focus. QApplication updates it's focus widget (QApplicationPrivate::setFocusWidget()) only when a focus event comes from the underlying system, window manager, or focus is set manually by calling QWidget::setFocus().
    J-P Nurmi

  16. The following user says thank you to jpn for this useful post:

    cocheci (5th June 2006)

  17. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Posting a QKeyEvent to a QLineEdit

    Maybe you could use QtTest module? It has some methods for sending key events.

  18. The following user says thank you to jacek for this useful post:

    cocheci (5th June 2006)

  19. #14
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posting a QKeyEvent to a QLineEdit

    Quote Originally Posted by jacek
    Maybe you could use QtTest module? It has some methods for sending key events.
    Thanks Jacek, it looks like this might work, I'll definitely try it. Here's the first phrase from the "Simulating GUI Event" chapter:

    QTestLib features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, QTestLib sends internal Qt events. That means there are no side-effects on the machine the tests are running on.

    If this works, I'll let you know.

    Cristian

  20. #15
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posting a QKeyEvent to a QLineEdit

    Quote Originally Posted by cocheci
    Thanks Jacek, it looks like this might work, I'll definitely try it. Here's the first phrase from the "Simulating GUI Event" chapter:

    QTestLib features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, QTestLib sends internal Qt events. That means there are no side-effects on the machine the tests are running on.

    If this works, I'll let you know.

    Cristian
    This worked, thank you all!!

    Cristian

Similar Threads

  1. a box around QLineEdit?
    By GreyGeek in forum Qt Tools
    Replies: 13
    Last Post: 8th February 2006, 15:40

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.