Find item in QApplication by only the objectname

后端 未结 3 599
南方客
南方客 2021-01-03 16:14

i want to find any object by a objectname string name inside of the QApplication

Something like

QApplication.instance().findByClassName(\"codeEditor         


        
3条回答
  •  迷失自我
    2021-01-03 16:52

    I came up with this which works quite well

    def getWidgetByClassName(name):
        widgets = QApplication.instance().topLevelWidgets()
        widgets = widgets + QApplication.instance().allWidgets()
        for x in widgets:
            if name in str(x.__class__).replace("",""):
                return x
    def getWidgetByObjectName(name):
        widgets = QApplication.instance().topLevelWidgets()
        widgets = widgets + QApplication.instance().allWidgets()
        for x in widgets:
            if str(x.objectName) == name:
                return x
    def getObjects(name, cls=True):
        import gc
        objects = []
        for obj in gc.get_objects():
            if (isinstance(obj, PythonQt.private.QObject) and
                ((cls and obj.inherits(name)) or
                 (not cls and obj.objectName() == name))):
                objects.append(obj)
        return objects
    

提交回复
热议问题