Reputation: 491
in the following executable code you can create more subwindow. But, when I switch from one subwindow to the other, I want the progrm detect which subwindow is current active. Image, we have opend two subwindow. One subwindow we call it A and the other subwindow we call it B. Both of them are already open. Subwindow A ist active, now I switch to Subwindow B. How can the class Custom_Window()
tell me "I am here and I am currently active"?.
UPDATE
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()
Upvotes: 0
Views: 1682
Reputation: 120638
The Qt docs for QMdiArea show you which APIs to use. There is a subWindowActivated signal that is designed for this purpose, so just connect a slot to it:
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)
Upvotes: 1