I struggled for a bit to figure this out, too...
First off, it is important to understand what is a UUID (or GUID)?
GUIDs are usually stored as 128-bit values, and are commonly displayed
as 32 hexadecimal digits with groups separated by hyphens, such as
{21EC2020-3AEA-1069-A2DD-08002B30309D}. GUIDs generated from random
numbers sometimes contain 6 fixed bits saying they are random and 122
random bits;
As far as QGIS is concerned, this would be a 36 character string that will look something like 3a2a47bf-ccaf-4aa9-8100-cb3abceec3e7
, so you would not be able to use a INT
field type.
Next, using the UUID Generator widget is a "manual" process that edits a single record at a time. To do so:
- Set your Properties-> Fields-> Edit Widget is set to "UUID generator".
- Open your attribute table
- Toggle on edit mode
- Double-click in the empty field (set in step 1) to generate a new UUID for that record.
If what you are really after is a way to automatically generate UUID's for all features, @NathanW modified this 2011 code to work with QGIS 2:
import uuid
fieldname = 'UUID' # set the name of your field to update
layer = qgis.utils.iface.activeLayer() # get the active layer from the TOC
layer.startEditing() # start editing
iter = layer.getFeatures() # get all features from the layer
for feat in iter: # iterate over features
feat[fieldname] = str(uuid.uuid4()) #fill field with GUID
layer.updateFeature(feat) #update the attribute value
layer.commitChanges() # save edits to the layer
Then, do the following:
- Highlight the layer to update (in the TOC)
- Open up the Python Console,
- Press the "Show Editor" button,
- Paste this code into the Python Console,
- Change the FIELDNAME variable to match the name of your field in your layer,
- Press the "Run script" button.