Qt4 no focus loss on clicking menubar

2019-07-16 16:52发布

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?

标签: qt qt4 focus pyqt
1条回答
神经病院院长
2楼-- · 2019-07-16 17:46

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.

查看更多
登录 后发表回答