from PyQt.QtCore import QVariant my_var = QVariant("hello world") print(my_var.toString())
from PyQt.QtCore import QVariant my_var = QVariant(42) my_var_int = my_var.toInt()[0] print(type(my_var_int), my_var_int)
from qgis.core import QgsFeature, QgsFields from PyQt.QtCore import QVariant fields = QgsFields() fields.append(QgsField("name", QVariant.String)) feature = QgsFeature(fields) feature.setAttribute("name", "John Doe")In this example, a QgsFields object is created and a field with a QVariant.String data type is added. This object is then used to create a QgsFeature object, which is populated with an attribute value using the `setAttribute()` method. This code example also requires the qgis.core package library. In conclusion, the use of QVariant in Python QGIS provides a convenient way to handle data types with flexibility and simplicity. It belongs to the PyQt.QtCore package library.