Reputation: 760
I hope I have a simple question. I have a pyqt mainwindow
that has an mdi
area for sub-windows. I would like to be able to get the text of the title from a current sub-window and set it to a variable.
The reason I am doing this is that when you click on other sections of my main window I have mdi
sub-windows that open up to edit the data that has been entered. I would like the user to be able to open and edit multiple sets of data at once and I am setting the directory key for the data in the title bar. I thought it would be a good way to differentiate between which set of data is being edited at the current moment.
I'm not sure if this is the best way or even a good way to achieve what I would like. If there is another method that would be better I would love to hear it.
Thank you for all your time.
Upvotes: 0
Views: 4715
Reputation: 20141
The QMdiArea
provides a method QMdiArea::activeSubWindow()
as well as a signal QMdiArea::subWindowActivated()
.
The QMdiSubWindow
is (directly) derived from QWidget
which in turn provides a property QWidget::windowTitle
.
Putting this altogether, it should work.
I prepared an MCVE as "proof of concept" (and to train my Python/PyQt skills).
Sample code testQMDIActiveSubWindow.py
:
#!/usr/bin/python3
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMdiArea, QMdiSubWindow
def updateActiveChild(subWindow):
win.setWindowTitle("MDI Test: '%s'" % subWindow.windowTitle())
if __name__ == '__main__':
app = QApplication(sys.argv)
# build GUI
win = QMainWindow()
win.resize(640, 480)
mdiArea = QMdiArea()
for title in ["Data:1", "Data:2", "Data:3", "Data:n"]:
mdiChild = QMdiSubWindow()
mdiChild.setWindowTitle(title)
mdiArea.addSubWindow(mdiChild)
mdiArea.tileSubWindows()
win.setCentralWidget(mdiArea)
win.show()
updateActiveChild(mdiArea.activeSubWindow())
# install signal handlers
mdiArea.subWindowActivated.connect(updateActiveChild)
# exec. application
sys.exit(app.exec_())
I tested it in python3, cygwin64, Windows 10 (64 bit):
The title of active sub-window is reflected in the title of the main window.
Upvotes: 3