5

I want to append chars to QLineEdit by sending KeyEvent. I'm using code like this:

ui.myEdit->setFocus();
for(size_t i = 0; i < 10; ++i) {
   QKeyEvent keyPressed(QKeyEvent::KeyPress, 'a', Qt::NoModifier);
   QWidget::keyPressEvent(&keyPressed); // or
   //QApplication::sendEvent(QApplication::focusWidget(), &keyPressed);
}

Why there is no change in myEdit?

Nejat
  • 31,784
  • 12
  • 106
  • 138
user3369485
  • 117
  • 1
  • 1
  • 7

3 Answers3

6

You can change the change the text of QLineEdit simply by :

ui->myEdit->setText(ui->myEdit->text().append("a"));

But if you really want to change it by sending QKeyEvent you can try this :

QKeyEvent * eve1 = new QKeyEvent (QEvent::KeyPress,Qt::Key_A,Qt::NoModifier,"a");
QKeyEvent * eve2 = new QKeyEvent (QEvent::KeyRelease,Qt::Key_A,Qt::NoModifier,"a");

qApp->postEvent((QObject*)ui->myEdit,(QEvent *)eve1);
qApp->postEvent((QObject*)ui->myEdit,(QEvent *)eve2);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Thank you, had to use fourth parameter, like this: QKeyEvent ev(QKeyEvent::KeyPress, ' ', Qt::NoModifier, 'a'); – user3369485 Jun 20 '14 at 11:41
  • 2
    Why are the casts required `(QObject*)( ... )` and `(QEvent*)( ... )` ? – p.i.g. May 28 '15 at 20:45
  • No, this is different behavior then OP. As demonstrated, `setText` always appends a character. OP's example will insert the character at QCursorPos (or whatever it is called). OP's insert can occur at the beginning, middle or end of the line. – jww Dec 17 '19 at 18:00
1

Your approach is not wise.

  1. Setting the focus yourself may annoy more than one user which loose focus from one UI element for the other.
  2. By calling keyPressEvent directly you are skipping many layers of processing from the framework. Only misbehavior await down this path.

To reply to

I want to append chars to QLineEdit

You can obtain the line edit text, modify at your will and set it back.

QString currentText = ui.myEdit->text();
QString toappend    = "aaaaaaaaaa";
QString nextText    = currentText + toappend;
ui.myEdit->setText(nextText);

or one line

ui.myEdit->setText(ui.myEdit->text()+mystring);
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
1

Synthesizing a key press event to append characters to a line edit is asking for endless trouble. You'd need to retain the state of the control to ensure that you are in fact appending characters. If the cursor is not at the end, you'll be inserting or prepending characters. If any modifiers are active, you may cause the widget to act as if, say, a clipboard shortcut was activated. Say if you "append" an X while Ctrl/⌘ is held down, you'll cause any selected text to disappear from the line edit.

In other words: if you want to append something to a textedit, simply append it, don't synthesize keystrokes.

lineEdit->setText(lineEdit->text() + "appended");

That's it. To do it properly via appending keystrokes requires about a page of code, and even then it can't but rely on Qt's implementation details.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313