我尝试了
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_())
这是一个非常简单的示例示例()()与Subwindow不起作用,但它可以与按钮和父级Mdiarea一起使用,我的目标是在这种情况下以subwindow_test_test_the_object_name的形式获取活动的subwindow对象名称,如果我有多个subwindows使用不同的对象名称,我可以将此对象名称作为信号获取。谢谢。
带有以下说明:
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow_test_the_object_name")
您将在QMdiSubWindow
内部而不是QMdiSubWindow
内给小部件的名称,因此,如果要检索该元素的名称,则必须使用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()))
输出:
there is a subwindow activated
this is subwindow 1 subwindow_test_the_object_name