MDI 中的 PyQt4 子窗口。检查哪个子窗口处于活动状态
PyQt4 Subwindow in MDI. Check which subwindow is active
在下面的可执行代码中你可以创建更多的子窗口。但是,当我从一个子窗口切换到另一个子窗口时,我希望程序检测哪个子窗口当前处于活动状态。图像,我们打开了两个子窗口。一个子窗口我们称之为 A,另一个子窗口我们称之为 B。它们都已经打开了。子窗口 A 已激活,现在我切换到子窗口 B。class Custom_Window()
怎么会告诉我 "I am here and I am currently active"?。
更新
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Custom_Window(QWidget) :
def __init__(self, parent = None) :
super(Custom_Window, self) .__init__(parent)
# print here in this class, that itself is currently active,
# because this window is a subwindow, so its inherits from class
# QMdiSubWindow(). I want this subwindow itself tell me
# whether its activated or not. BUT I don't know how to
# solve this idea.
def changeEvent(self, event):
print "event", event
print "type", type(self.parent())
print "Status", self.parent().isActiveWindow()
if event.type() == QEvent.WindowActivate:
if self.parent().isActiveWindow():
print "is currently active."
else: print "I am passive"
class MainWindow(QMainWindow) :
count = 0
def __init__(self, parent = None) :
super(MainWindow, self) .__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.addAction("cascade")
file.addAction("Tiled")
file.triggered[QAction].connect(self.windowaction)
self.setWindowTitle("MDI demo")
def windowaction(self, q) :
custom_window = Custom_Window()
print "triggered"
if q.text() == "New" :
MainWindow.count = MainWindow.count+1
sub = QMdiSubWindow()
sub.setWidget(Custom_Window() )
sub.setWindowTitle("subwindow"+str(MainWindow.count) )
self.mdi.addSubWindow(sub)
sub.show()
if q.text() == "cascade" :
self.mdi.cascadeSubWindows()
if q.text() == "Tiled" :
self.mdi.tileSubWindows()
def main() :
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_() )
if __name__ == '__main__':
main()
QMdiArea show you which APIs to use. There is a subWindowActivated 信号的 Qt 文档就是为此目的而设计的,因此只需将插槽连接到它即可:
class Custom_Window(QWidget) :
def handleActivationChange(self, subwindow):
if subwindow is self.parent():
print 'activated:', self
else:
print 'deactivated:', self
...
self.mdi.subWindowActivated.connect(
sub.widget().handleActivationChange)
在下面的可执行代码中你可以创建更多的子窗口。但是,当我从一个子窗口切换到另一个子窗口时,我希望程序检测哪个子窗口当前处于活动状态。图像,我们打开了两个子窗口。一个子窗口我们称之为 A,另一个子窗口我们称之为 B。它们都已经打开了。子窗口 A 已激活,现在我切换到子窗口 B。class Custom_Window()
怎么会告诉我 "I am here and I am currently active"?。
更新
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Custom_Window(QWidget) :
def __init__(self, parent = None) :
super(Custom_Window, self) .__init__(parent)
# print here in this class, that itself is currently active,
# because this window is a subwindow, so its inherits from class
# QMdiSubWindow(). I want this subwindow itself tell me
# whether its activated or not. BUT I don't know how to
# solve this idea.
def changeEvent(self, event):
print "event", event
print "type", type(self.parent())
print "Status", self.parent().isActiveWindow()
if event.type() == QEvent.WindowActivate:
if self.parent().isActiveWindow():
print "is currently active."
else: print "I am passive"
class MainWindow(QMainWindow) :
count = 0
def __init__(self, parent = None) :
super(MainWindow, self) .__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.addAction("cascade")
file.addAction("Tiled")
file.triggered[QAction].connect(self.windowaction)
self.setWindowTitle("MDI demo")
def windowaction(self, q) :
custom_window = Custom_Window()
print "triggered"
if q.text() == "New" :
MainWindow.count = MainWindow.count+1
sub = QMdiSubWindow()
sub.setWidget(Custom_Window() )
sub.setWindowTitle("subwindow"+str(MainWindow.count) )
self.mdi.addSubWindow(sub)
sub.show()
if q.text() == "cascade" :
self.mdi.cascadeSubWindows()
if q.text() == "Tiled" :
self.mdi.tileSubWindows()
def main() :
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_() )
if __name__ == '__main__':
main()
QMdiArea show you which APIs to use. There is a subWindowActivated 信号的 Qt 文档就是为此目的而设计的,因此只需将插槽连接到它即可:
class Custom_Window(QWidget) :
def handleActivationChange(self, subwindow):
if subwindow is self.parent():
print 'activated:', self
else:
print 'deactivated:', self
...
self.mdi.subWindowActivated.connect(
sub.widget().handleActivationChange)