我是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
。但没有任何进步。我仍然无法解决这个问题。
有人可以就这个问题提出宝贵意见吗?
你的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)