1

This is the error I am getting when trying to run my plugin.

"NameError: name 'QVariant' is not defined" This same code will work in the python console in QGIS. So, I don't have an idea as to why it won't work in the plugin that I am making.

from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, QFileDialog
from qgis.core import *
from qgis.core import QgsField

def Create_fields(self, layer):

    list_fields = []

    for field in layer.fields():
        field_id = field.name()
        list_fields.append(field_id)

    # Adding fields to shapefile

    if len(list_fields) == 1:
        res = layer.dataProvider().addAttributes([
            QgsField("Avg_NDVI", QVariant.Double),
            QgsField("Date", QVariant.Date),
            QgsField("Study_Type", QVariant.Int),
            QgsField("Trial_Num", QVariant.Int),
            QgsField("Plot_Num", QVariant.Int),
            QgsField("Trt", QVariant.Int),
            QgsField("Rep", QVariant.Int)])
    layer.commitChanges()

edit

The fix was changing the qVersion to QVariant in the import statement area.

  • 2
    It looks like you haven't actually imported the QVariant class, and you have an unused import 'QVersion'. What happens if you change QVersion to QVariant in your import statement? – Ben W Jun 13 '18 at 22:26
  • No worries, glad it helped. – Ben W Jun 14 '18 at 0:16
  • @BenW - Please post your comment as an answer :) – Joseph Jun 14 '18 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.