What you try at the end is the correct way to do it. The only weird thing I see is window_vt = QMdiSubWindow(self.win_vt)
that should be window_vt = QMdiSubWindow()
. If not, the cause of not working for you may be due to name conflict. Keep in mind that just as it is window_ts
, window_vt
and window_norm
are local variables to the method where you define them. If you use self.mdiArea.setActiveSubWindow(window_ts)
outside of that method it will not work logically. On the other hand, using the same name for the instances of your widgets and for those of QMdiSubWindow
can be confusing and even lead to errors. You should see your entire app or at least the error it sends you (if it does).
That said, I'll explain a bit how to do it and give a reproducible example to show its use.
You must create QMdiSubWindows
within QMdiarea
, then add the widgets you want to each subwindow as if it were any other window (as we would in a QMainWidget
for example).
The setActiveSubWindow
method must receive an instance of QMdiSubWindows
in all cases.
In the following example, three windows are created and we place the focus on the second one that is created. Each window has a QTextEdit
inside, this is only as an example and so that they are not empty windows. It would be the same for any other frame.
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QMdiSubWindow, QMdiArea, QTextEdit
class MainWindow(QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("MDI demo")
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
self.win_ts = QMdiSubWindow()
self.win_ts.setWindowTitle("Window TS")
self.win_ts.setWidget(QTextEdit())
self.win_vt = QMdiSubWindow()
self.win_vt.setWindowTitle("Window VT")
self.win_vt.setWidget(QTextEdit())
self.win_norm = sub = QMdiSubWindow()
self.win_norm.setWindowTitle("Window Norm")
self.win_norm.setWidget(QTextEdit())
self.mdi.addSubWindow(self.win_ts)
self.mdi.addSubWindow(self.win_vt)
self.mdi.addSubWindow(self.win_norm)
self.win_ts.show()
self.win_vt.show()
self.win_norm.show()
self.mdi.cascadeSubWindows() # Ordena las ventanas en cascada
self.mdi.setActiveSubWindow(self.win_vt) #<<<<<<<<
def main():
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
What should produce something like:
We can see how the focus appears in the second window ( self.win_vt
) as we wanted.