简体   繁体   English

如何获取Qt小部件类型

[英]How to get Qt widget type

a = QtGui.QLineEdit()   

Is there a way to query what Qt widget the variable a is? 有没有办法查询变量a是什么Qt小部件? It appears some Qt widgets would be supplied with .uiType attribute. 似乎某些Qt小部件将提供.uiType属性。 But some (like layouts) are not. 但是有些(如布局)则不是。

You can get the type of any object in Python using the type built-in : 您可以使用内置类型获取Python中任何对象的type

print type(a)

You may also be interested in the __name__ and __class__ attributes, which will hold data corresponding to the widget's name and class: 您可能还对__name____class__属性感兴趣,这些属性将保存与小部件的名称和类相对应的数据:

type(a).__name__ # Returns class name as a string
a.__class__      # Same as type(a)

Note however that if you need to perform a typecheck , you should use isinstance : 但是请注意,如果你需要执行一个类型检测 ,你应该使用isinstance

isinstance(a, type_to_test_for)

This will take into account inheritance. 这将考虑继承。

Have you tried a.metaObject().className() ? 您是否尝试过a.metaObject().className()

See the QObject documentation for more info. 有关更多信息,请参见QObject文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM