【问题标题】:Getting keyPressEvent in QLineEdit in Qt在 Qt 中的 QLineEdit 中获取 keyPressEvent
【发布时间】:2011-05-17 11:15:45
【问题描述】:

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

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

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

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

同样在输入密钥后,如果我在QLineEdit 框外单击,它仍然可见。但是现在QLineEdit里面也不能输入key,也就是说,输入key之后,我必须在QlineEdit外面点一下,才能在QLineEdit里面显示输入的key。

我试过了:

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

我有一个keyPressEvent(); 函数。我尝试在按下键时显示QlineEdit。 但没有任何改善。我仍然无法解决这个问题。

任何人都可以就这个问题提供有价值的建议吗?

【问题讨论】:

  • 我真的不知道这里问的是什么,要么你想做什么,要么什么不起作用。

标签: qt4


【解决方案1】:

您的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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2014-08-10
    • 2017-02-14
    相关资源
    最近更新 更多