I have a mdiArea
to which I add several windows (of the class QWidget
):
self.mdiArea.addSubWindow(self.win_ts)
self.mdiArea.addSubWindow(self.win_vt)
self.mdiArea.addSubWindow(self.win_norm)
By default, the last window added is the active one ( win_norm
), but I want it to be the first ( win_ts
), for this I have seen the method setActiveSubWindow
but it receives a y as a parameter QMdiSubWindow
when doing:
self.mdiArea.setActiveSubWindow(self.win_ts)
throws an error, obviously.
I've tried to create QmdiSubWindows from QWidgets, but it doesn't seem to work either:
window_ts = QMdiSubWindow()
window_ts.setWidget(self.win_ts)
window_vt = QMdiSubWindow(self.win_vt)
window_vt.setWidget(self.win_vt)
window_norm = QMdiSubWindow()
window_norm.setWidget(self.win_norm)
self.mdiArea.addSubWindow(window_ts)
self.mdiArea.addSubWindow(window_vt)
self.mdiArea.addSubWindow(window_norm)
self.mdiArea.setActiveSubWindow(window_ts)
How can I then set the active window?
I'm relatively new to PyQt, so any suggestions are helpful.
What you try in the end is the correct way to do it. The only weird thing I see is
window_vt = QMdiSubWindow(self.win_vt)
that it should bewindow_vt = QMdiSubWindow()
. If not, the cause of it not working for you may be due to a name conflict. Note that as-iswindow_ts
,window_vt
andwindow_norm
are local variables to the method where you define them. If you useself.mdiArea.setActiveSubWindow(window_ts)
outside of said method it won't work logically. On the other hand, using the same name for your and your widget instancesQMdiSubWindow
can be confusing and even misleading. It would be necessary to see your complete app or at least the error that throws you (if it does).Having said that, I am going to explain a little how it should be done and give a reproducible example to show its use.
You must create
QMdiSubWindows
inside theQMdiarea
, then add the widgets you want to each subwindow as if it were any other window (as we would in oneQMainWidget
for example).The method
setActiveSubWindow
must receive an instance ofQMdiSubWindows
in any case.In the following example, three windows are created and we place the focus on the second one that is created. Each window has an
QTextEdit
inside, this is just as an example and so that they are not empty windows. It would be the same for any other frame.Which should produce something like:
We can see how the focus appears in the second window (
self.win_vt
) just as we wanted.