QLineEdit uses QKeyEvent::text() for inserting text.
Qt Code:
Qt::Key_A, Qt::NoModifier, "a")); // <-QKeyEvent::textTo copy to clipboard, switch view to plain text mode
QLineEdit uses QKeyEvent::text() for inserting text.
Qt Code:
Qt::Key_A, Qt::NoModifier, "a")); // <-QKeyEvent::textTo copy to clipboard, switch view to plain text mode
J-P Nurmi
cocheci (31st May 2006)
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.Originally Posted by wysota
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
Because it is QFocusEvent's responsibility.
cocheci (2nd June 2006)
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
cocheci (2nd June 2006)
Originally Posted by jpn
Originally Posted by wysota
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?
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.Originally Posted by cocheci
I tried that, but it didn't quite work as in interactive mode. The LineEdit receives 4 events when the user clicks on it:Originally Posted by jacek
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
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
cocheci (5th June 2006)
Maybe you could use QtTest module? It has some methods for sending key events.
cocheci (5th June 2006)
Thanks Jacek, it looks like this might work, I'll definitely try it. Here's the first phrase from the "Simulating GUI Event" chapter:Originally Posted by jacek
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!!Originally Posted by cocheci
Cristian
Bookmarks