PyQt窗口焦点 [英] PyQt Window Focus
问题描述
如果用户单击另一个窗口,我试图将焦点放在一个窗口上.
I am trying to give focus to a window if the user clicks on another window.
现在我有两个窗口:窗口A在后面,窗口B在前面.当窗口B出现时,它禁用了窗口A.现在我想要的是,每当用户单击窗口B之外时,它都应将焦点重新分配给窗口B.
Right now i have two windows: Window A is behind, and Window B is in front. When Window B appears, it disables Window A. Now what i want is that whenever the user clicks outside of Window B, it should give focus back to Window B.
这是窗口B的代码:
class window_b(QtGui.QDialog):
def __init__(self,parent=None):
super(window_b, self).__init__(parent)
window_a.setEnabled(False)
self.ui = Ui_Form_window_b()
self.ui.setupUi(self)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
def focusOutEvent(self,event):
self.setFocus(True)
self.activateWindow()
self.raise_()
self.show()
我尝试了setFocus
和activateWindow
,但是并没有将焦点返回到窗口B.
I tried setFocus
and activateWindow
, but it didnt give focus back to Window B.
有什么建议吗?
推荐答案
要使window_b
始终保持在顶部,您必须添加windowflag QtCore.Qt.WindowStaysOnTopHint
.在您的__init__
中添加通话
To get window_b
to always stay on top you have to add the windowflag QtCore.Qt.WindowStaysOnTopHint
. In your __init__
add the call
self.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint)
我必须补充一点,这仅是窗口管理器的提示,不能保证成功.
I have to add that this only is a hint to the windowing manager and not guaranteed to succeed.
这篇关于PyQt窗口焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!