简体   繁体   English

QT QcoreApplication postEvent()行为

[英]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.

相关问题 QCoreApplication :: postEvent的异常安全保证 - Exception safety guarantee of QCoreApplication::postEvent 转发从QCoreApplication :: postEvent收到的QEvent是否安全? - Is it safe to forward a QEvent recieved from QCoreApplication::postEvent 如何使用 QCoreApplication::postEvent 注入合成输入事件 - How to use QCoreApplication::postEvent to inject synthetic input events Qt QCoreApplication addLibraryPath使用 - Qt QCoreApplication addLibraryPath use Qt蓝牙服务器无法与QCoreApplication一起使用 - Qt Bluetooth server not working with QCoreApplication Qt 5.1.0中是否还支持QCoreApplication的aboutToQuit()信号? - Is the aboutToQuit() signal of the QCoreApplication still supported in Qt 5.1.0? Qt:将QCoreApplication / QNetworkAccessManager与共享动态C ++库结合使用 - Qt: Using QCoreApplication / QNetworkAccessManager with Shared Dynamic C++ Library QT5 错误:未知类型名称“QCoreApplication”。 Qt5 中的第一个程序 - QT5 error: Unknown type name 'QCoreApplication'. First program in Qt5 Qt 调试:我如何知道 QCoreApplication::notifyInternal2 正在向哪个对象发送消息? - Qt debugging: How can I know which object QCoreApplication::notifyInternal2 is sending a message to? Qt-跨平台行为 - Qt - cross platform behaviour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM