问题描述
但是我有一个奇怪的问题,其中只有一个窗口实际上获取了focusIn和focusOut事件,而其他窗口只在创建时接收到一个,否则从来没有收到他们。下面是一个示例程序:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b从PyQt4.QtCore导入*
从PyQt4.QtGui导入*
导入sys
类MyWindow(QMainWindow):
def __init __(self):
super(MyWindow,self).__ init __()
self.label = QLabel('Window')
self.setCentralWidget(self.label)
self .setFocusPolicy(Qt.StrongFocus)
$ b $ def focusInEvent(self,event):
self.label.setText('got focus')
$ b $ def focusOutEvent(self ,event):
self.label.setText('Lost focus')
def main():
app = QApplication(sys.argv)
win1 = MyWindow()
win2 = MyWindow()
win1.show()
win2.show()
sys.exit(app.exec_())
if __name__ =='__main__':
main()
从PyQt4.QtCore导入*
from PyQt4.QtGui import *
import sys
class MyWindow(QMainWindow):
def __init __(self,parent = None):
(self.label)
self.setFocusPolicy(Qt.StrongFocus)$ b()
$ def focusInEvent(self,event):
self.label.setText('got focus')
$ b $ def def focusOutEvent(self,event):
self .black.setText('Lost focus')
$ b $ def changedFocusSlot(old,now):
if(now == None and QApplication.activeWindow()!= None):
打印设置焦点到活动窗口
QApplication.activeWindow()。setFocus()
def main():
app = QApplication(sys.argv)
QObject.connect(app,SIGNAL(focusChanged(QWidget *,QWidget *)),changedFoc usSlot)
win1 = MyWindow()
win2 = MyWindow()
win1.show()
win2.show()
sys.exit ()
$ b 希望这可以帮助,关心
I have a PyQt4 program where I'm trying to get notified when a window gets focus, following the advice in the QUndoGroup docs:
But I have a weird problem where only one window actually gets the focusIn and focusOut events, while the others either receive only one at their creation, or else never receive them at all. Here is an example program:
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.label = QLabel('Window')
self.setCentralWidget(self.label)
self.setFocusPolicy(Qt.StrongFocus)
def focusInEvent(self, event):
self.label.setText('Got focus')
def focusOutEvent(self, event):
self.label.setText('Lost focus')
def main():
app = QApplication(sys.argv)
win1 = MyWindow()
win2 = MyWindow()
win1.show()
win2.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
解决方案 I'm actually not quite sure why it doesn't work, might be an issue in how qt handles focus transition between windows. Anyways, below is how you might fix this, I've changed your code a bit
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyWindow(QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__()
self.label = QLabel('Window')
self.setCentralWidget(self.label)
self.setFocusPolicy(Qt.StrongFocus)
def focusInEvent(self, event):
self.label.setText('Got focus')
def focusOutEvent(self, event):
self.label.setText('Lost focus')
def changedFocusSlot(old, now):
if (now==None and QApplication.activeWindow()!=None):
print "set focus to the active window"
QApplication.activeWindow().setFocus()
def main():
app = QApplication(sys.argv)
QObject.connect(app, SIGNAL("focusChanged(QWidget *, QWidget *)"), changedFocusSlot)
win1 = MyWindow()
win2 = MyWindow()
win1.show()
win2.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
hope this helps, regards
这篇关于没有调用PyQt窗口焦点事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!