没有调用PyQt窗口焦点事件 [英] PyQt window focus events not called

查看:1056
本文介绍了没有调用PyQt窗口焦点事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个PyQt4程序,当我试图在窗口获得焦点时得到通知时,按照QUndoGroup文档中的建议进行操作。程序员有责任通过调用QUndoStack :: setActive()来指定哪个堆栈处于活动状态,通常是当关联的文档窗口获得焦点时。

但是我有一个奇怪的问题,其中只有一个窗口实际上获取了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()


<解决方案我实际上不太清楚为什么它不起作用,可能是qt如何处理窗口间焦点转换的问题。无论如何,下面是你如何解决这个问题,我已经改变了你的代码

 从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:

It is the programmer's responsibility to specify which stack is active by calling QUndoStack::setActive(), usually when the associated document window receives focus.

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窗口焦点事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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