[英]QT QcoreApplication postEvent() behaviour
I have written this simple QT mainwindow, only if I pass QString argument to the QKeyEvent, it prints the key, I expect the key to be printed even without QString argument? 我已经编写了这个简单的QT主窗口,仅当我将QString参数传递给QKeyEvent时,它才会打印键,我希望即使没有QString参数也能打印键?
section 1 in below code does not seem to work (I don't get the key printed in the QLineEdit field; while section 2 works and "1" is printed! is this normal behavior? what happens to the event when its posted in first section of the code? 下面的代码中的第1部分似乎无效(我没有在QLineEdit字段中打印密钥;而第2部分有效并且打印了“ 1”!这是正常现象吗?在第一次发布事件时会发生什么情况?代码部分?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->ui->lineEdit->setFocus();
Qt::Key key = Qt::Key_1;
// 1
QKeyEvent *event = new QKeyEvent (QEvent::KeyPress, key ,Qt::NoModifier);
QCoreApplication::postEvent(QWidget::focusWidget(), event); // Does not work! No key is set in the widget
//
//2
QKeyEvent *event2 = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString());
QCoreApplication::postEvent(QWidget::focusWidget(), event2); // this one works!
}
Not all key-events have a text representation (delete, curser movement, shortcuts, ...). 并非所有按键事件都具有文本表示形式(删除,光标移动,快捷方式等)。 For those who have one, the
QKeyEvent
class stores it in its text. 对于那些拥有一个的人,
QKeyEvent
类将其存储在其文本中。 You have to provide that text, otherwise its a "textless" event. 您必须提供该文本,否则提供“无文本”事件。
QLineEdit
will just add the text, and not deduce it from the event type (as can be seen here ) QLineEdit
将刚刚添加的文本,而不是从事件类型推断它(可以看出这里 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.