Search code examples
pythonpyqtpyqt5qwidgetqmdiarea

How to get the objectName of MDI Area subwindow?


I have tried

self.mdiArea.activeSubWindow().objectName
self.mdiArea.activeSubWindow().objectName()

But neither of them gave me the object name I set:

self.subwindow.setObjectName("subwindow_1")

Any advice? Thanks a lot. Full testing code is attached:

# Created by: PyQt5 UI code generator 5.6

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(687, 438)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
        self.mdiArea.setGeometry(QtCore.QRect(140, 10, 531, 381))
        self.mdiArea.setObjectName("mdiArea")
        self.subwindow = QtWidgets.QWidget()
        self.subwindow.setObjectName("subwindow_test_the_object_name")
        self.Add = QtWidgets.QPushButton(self.centralwidget)
        self.Add.setGeometry(QtCore.QRect(10, 30, 121, 23))
        self.Add.setObjectName("Add")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 687, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)       
        self.Add.clicked.connect(self.Add_window) # connect the button to Add_window function
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def Add_window(self):   
        self.mdiArea.addSubWindow(self.subwindow)
        print("there is a subwindow activated")
        self.mdiArea.activeSubWindow()
        self.subwindow.show()
        Act_window=self.mdiArea.activeSubWindow()
        print("this is subwindow 1 "+str(Act_window.objectName()))
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.subwindow.setWindowTitle(_translate("MainWindow", "Subwindow"))
        self.Add.setText(_translate("MainWindow", "Add Window"))
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_()) 

This is a very simple sample with objectName() does not work with subwindow but it works with button and the parent MDIarea, my goal is to get the active subwindow object name such as subwindow_test_the_object_name in this case, if I have multiple subwindows with different object name, I can grab this object name as a signal. Thanks.


Solution

  • With the following instruction:

    self.subwindow = QtWidgets.QWidget()
    self.subwindow.setObjectName("subwindow_test_the_object_name")
    

    you are giving a name to the widget that is going to be inside the QMdiSubWindow, not the QMdiSubWindow, so if you want to retrieve the name of that element you must use the widget() method of QMdiSubWindow:

    def Add_window(self):   
        self.mdiArea.addSubWindow(self.subwindow)
        print("there is a subwindow activated")
        self.mdiArea.activeSubWindow()
        self.subwindow.show()
        Act_window =self.mdiArea.activeSubWindow()
        print("this is subwindow 1 "+str(Act_window.widget().objectName()))
    

    Output:

    there is a subwindow activated
    this is subwindow 1 subwindow_test_the_object_name