在Qt中的QLineEdit中获取keyPressEvent

问题描述 投票:2回答:1

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

在我的应用程序中,有一个QWidget包含两个pushbuttons和一个QLineEdit。最初QLineEdit是隐藏的。

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

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

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

我尝试过:

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

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

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

qt4
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)
© www.soinside.com 2019 - 2024. All rights reserved.