如何获取MDI Area子窗口的objectName?

时间:2017-11-30 22:23:18

标签: python pyqt pyqt5 qwidget qmdiarea

我试过了

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

但是他们都没有给我我设置的对象名称:

self.subwindow.setObjectName("subwindow_1")

有什么建议吗?非常感谢。附上完整的测试代码:

# 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_()) 

这是一个非常简单的示例,objectName()不适用于子窗口,但它适用于按钮和父MDIarea,我的目标是在这种情况下获取活动子窗口对象名称,例如subwindow_test_the_object_name,如果我有多个子窗口使用不同的对象名称,我可以将此对象名称作为信号。感谢。

1 个答案:

答案 0 :(得分:0)

使用以下说明:

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

您要给位于QMdiSubWindow内的小部件命名,而不是QMdiSubWindow,所以如果要检索该元素的名称,则必须使用{{1 QMdiSubWindow的方法:

widget()

输出:

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()))