问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/22082582/qt4-no-focus-loss-on-clicking-menubar