QApplication.allWidgets()函数详细教程


PySide6.QtWidgets.QApplication类的成员函数allWidgets()用于返回一个包含所有小部件对象的列表;

示例代码

import sys
from PySide6.QtWidgets import QApplication,QWidget,QPushButton,QVBoxLayout
from PySide6.QtCore import Slot

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

        self.button = QPushButton("打印所有小部件")
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.button)
        self.button.clicked.connect(self.printAllWidgets)

    @Slot()
    def printAllWidgets(self):
        for widget in app.allWidgets():#遍历返回的小部件对象列表
            print(widget)

app = QApplication([])
widget = MyWidget()
widget.resize(300, 200)
widget.show()
sys.exit(app.exec())

如需转载,请注明出处;本文地址:https://www.perfcode.com/p/pyside6-qtwidgets-qapplication-allwidgets.html