简体   繁体   English

QLineEdit不使用QKeyEvent更新文本

[英]QLineEdit not updating text with QKeyEvent

I'm trying to implement a virtual keyboard widget. 我正在尝试实现虚拟键盘小部件。 The simplest way I could think of is to create QKeyEvent instances and send them with QApplication.postEvent() to the widget in focus. 我能想到的最简单的方法是创建QKeyEvent实例并将它们与QApplication.postEvent()一起发送到焦点小部件。

First, I'm trying to update a fixed QLineEdit that I have, so the code is: 首先,我正在尝试更新我拥有的固定QLineEdit,因此代码为:

   self.pushButton_A.clicked.connect(self.virtualKeyPress)

[...]

   def virtualKeyPress(self):
        self.keyPress = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier)
        QApplication.postEvent(self.lineEdit, self.keyPress)

But the QLineEdit instance won't update its text in the GUI! 但是QLineEdit实例不会更新GUI中的文本!

Clues? 线索? Cheers and thanks! 干杯谢谢!

RESOLVED: (kudos to HeyYO ) 决议:(HeyYO的称赞

   self.pushButton_A.clicked.connect(self.virtualKeyPress)

[...]

   def virtualKeyPress(self):
        self.keyPress = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier, 'A')
        QApplication.postEvent(self.lineEdit, self.keyPress)

In my case, inplace of Qt.Key_A I set that argument to 0 so that I can connect all my buttons to the virtualKeyPress method. 在我的情况下,在Qt.Key_A的位置我将该参数设置为0,以便我可以将所有按钮连接到virtualKeyPress方法。 I also had to set the focus policy for all the buttons to 'no focus' (did it directly in Qt Designer). 我还必须将所有按钮的焦点策略设置为“无焦点”(直接在Qt Designer中完成)。 The final code was the following: 最终的代码如下:

def virtualKeyPress(self):
    self.keyPressed = QString(self.sender().text())
    self.keyPress = QKeyEvent(QEvent.KeyPress, 0, Qt.NoModifier, self.keyPressed)
    self.focusWidget = QApplication.focusWidget()        
    QApplication.postEvent(self.focusWidget, self.keyPress)

Have you tried specifying the text argument; 您是否尝试过指定text参数;

self.keyPress = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier, "A")

It worked for me, in Qt5&C++, so I'm assuming it will work for you as well. 它在Qt5和C ++中对我有用,所以我认为它也适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM