Qt4单击菜单栏不会造成重点损失 [英] Qt4 no focus loss on clicking menubar

查看:164
本文介绍了Qt4单击菜单栏不会造成重点损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pyqt应用程序,我有两个QLineEdit字段从QDataWidgetMapper获取数据。我可以编辑字段中的文本,而当我例如点击另一个未被选中的字段。



但是,如果我编辑文本,然后点击菜单栏,保存,我不会有焦点丢失,因此我上次输入的数据永远不会到达模型。$ ​​b
$ b

如何在单击菜单栏时获得焦点损失? / p>

解决方案

对于我来说,连接到 QLineEdit.editingFinished 信号按预期工作。也就是说,无论何时按下return / enter,信号都会被发送,或者线路编辑失去焦点(包括打开菜单,最小化窗口,关闭程序等等)。

然而从长远来看,我认为单靠这个机制是一个坏主意。给出一个例子,说明为什么这可能是:如果你在未来的某个时候为菜单项添加了键盘快捷方式?通过键盘快捷键激活保存操作不会触发焦点更改事件...

所以我会设置一个单独的机制,可以以编程方式在调用save-action之前提交所有挂起的更改。


I have a pyqt application where I have two QLineEdit fields which get data from a QDataWidgetMapper. I can edit the text in the fields and the change is commit on focus loss when I e.g. click in the other unselected field.

But if I edit the text and then click on the menubar and there on e.g. save, I won't have a focus loss and therefore my last entered data never reaches the model.

How can I get a focus loss on clicking on the menubar?

解决方案

For me, connecting to the QLineEdit.editingFinished signal works as expected. Which is to say, the signal is sent whenever return/enter is pressed, or the line-edit loses focus (which includes opening a menu, minimizing the window, closing the program, etc).

However, in the long-term, I think relying on this mechanism alone is a bad idea. To give one example of why this may be: what if you added keyboard shortcuts for your menu items at some point in the future? Activating the save-action via a keyboard shortcut isn't going to trigger a focus-change event...

So I would put in place a separate mechanism that can programmatically committ all pending changes before invoking the save-action.

这篇关于Qt4单击菜单栏不会造成重点损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆