当MainWindow失去焦点时,我试图减小App的高度,但是下面的代码没有发生这种情况。
class MyApp(QMainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.installEventFilter(self)
time_frame_values = [i/30 for i in range(30)]
self.timeFrame = map(lambda x: x**0.5, time_frame_values)
def eventFilter(self, widget, event):
if event.type() == QEvent.FocusOut:
self.on_focus_out()
print("Focus Out!")
return False
def on_focus_out(self):
print("Focus Out!!")
for i in reversed(self.timeFrame):
self.move(QPoint(ScreenSizeObject.width()-self.width()-10, ScreenSizeObject.height()-self.original_height-(i*300)))
self.ui.textDisplay.resize(QSize(self.ui.textDisplay.width(), self.ui.textDisplay.height() + (i * 15)))
time.sleep(0.01)
此处self.on_focus_out()
将产生过渡效果。
期望:当我单击主窗口中的任意位置时,我希望self.on_focus_out()
运行。
可靠性:没有过渡效果,甚至print('Focus Out!')
都不打印字符。
感谢您的努力!
答案 0 :(得分:0)
对于所需的行为,
将QEvent.FocusOut
更改为QEvent.WindowDeactivate
,问题就解决了!
重写正确的代码,
class MyApp(QMainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.installEventFilter(self)
time_frame_values = [i/30 for i in range(30)]
self.timeFrame = map(lambda x: x**0.5, time_frame_values)
def eventFilter(self, widget, event):
if event.type() == QEvent.WindowDeactivate:
self.on_focus_out()
print("Focus Out!")
return False
def on_focus_out(self):
print("Focus Out!!")
for i in reversed(self.timeFrame):
self.move(QPoint(ScreenSizeObject.width()-self.width()-10, ScreenSizeObject.height()-self.original_height-(i*300)))
self.ui.textDisplay.resize(QSize(self.ui.textDisplay.width(), self.ui.textDisplay.height() + (i * 15)))
time.sleep(0.01)