Set the active window in a QmdiArea

2

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 one ( win_ts ), for this I have seen the method setActiveSubWindow but receive as parameter a QMdiSubWindow and when doing:

self.mdiArea.setActiveSubWindow(self.win_ts)

throws error, obviously.

I have started creating QmdiSubWindows from the QWidgets, but it does not 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.

    
asked by Jose Hermosilla Rodrigo 13.05.2017 в 18:27
source

1 answer

1

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.

    
answered by 14.05.2017 / 05:15
source