首页 文章

在Qt中的QLineEdit中获取keyPressEvent

提问于
浏览
2

我是Qt的新手 . 我正在使用Qt4.7与linux操作系统 . 我的应用程序编译为嵌入式mipsel设备 .

在我的应用程序中,有一个包含两个按钮和一个QLineEdit的QWidget . 最初隐藏QLineEdit .

我的要求是:当我在应用程序的键盘上按下某个键时,应该显示QlineEdit并通过该键输入 . 之后,它应该采取所有关键输入 . 同时它不显示光标闪烁 .

但是,按下该键时,我的应用程序无法显示QlineEdit .

输入密钥后,如果我在QLineEdit框外单击,它仍然可见 . 但现在我也无法在QLineEdit中输入密钥,即在输入密钥后,我必须在QlineEdit的外部单击以在QLineEdit中显示输入的密钥 .

我尝试过:

QLineEdit->setFocusPolicy(Qt::StrongFocus);
this->setFocusPolicy(Qt::StrongFocus);

我有一个keyPressEvent();功能 . 在那里我尝试在按下键时显示QlineEdit . 但没有任何进步 . 我仍然无法解决这个问题 .

有人可以就这个问题提出宝贵意见吗?

如果您有任何想法,请与我分享 .

提前致谢 .

1 回答

  • 0

    你的 keyPressEvent 是否包含 QWidget ?如果是这样,我想它可能会在进入QLineEdit之前吃完所有按键

    如果是这种情况,你可以使用 QWidget.keyPressEvent 来简单地聚焦QLineEdit,如果它是散焦的 . 在伪代码中:

    class MyContainer(QWidget):
        def keyPressEvent(event):
            if my_qlineedit.isFocused():
                # Do nothing, call default implementation, allowing
                # key-presses to be passed to QLineEdit normally
                super().keyPressEvent(event)
                return
    
            else:
                # Show QLineEdit (for first keystroke)
                my_qlineedit.setVisible(True)
    
                # Set focus for future key strokes to be sent directly to the QLineEdit
                my_qlineedit.setFocused(True)
    
                # Send this key-event to avoid missing a key
                my_qlineedit.keyPressedEvent(event)
    

相关问题