9

I have to draw several stuff and input the data, all the layers have the same data fields but some require 2\5 attributes to be filled.

Is there a way to change the order of those fields to be on top so I get to input them easier without changing the order of the field themselves?

Taras
  • 32,823
  • 4
  • 66
  • 137
AsdAsd
  • 489
  • 2
  • 6
  • 14
  • 2
    Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our [faq]. – underdark May 13 '17 at 07:35

5 Answers5

18

Open the attribute table. Right-click the column headings in the attribute table. Select "Organize columns" in the drop-down menu.

Then, just drag the heading to the position you want it to be in (See the image of the "organize table columns" box below).

Click OK. Headings should have rearranged the way you want them to be arranged in the attribute table.

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
Angela P
  • 361
  • 3
  • 2
15

Open the Fields tab from the Layer properties. From there, select the Drag and drop designer option and drag to the right the fields you want to edit. You can also change the field order.

You can then edit a feature and choose to display the fields in form view. Only the fields you selected in the previous step will be shown.

Layer property, field tab

Restrict fields to be edited

Taras
  • 32,823
  • 4
  • 66
  • 137
JGH
  • 41,794
  • 3
  • 43
  • 89
  • 1
    Is it possible to do this programmatically via PyQgis API? – Vladimir Obrizan May 12 '22 at 05:41
  • 1
    Ok, found https://qgis.org/pyqgis/master/core/QgsEditFormConfig.html which responsible for that. – Vladimir Obrizan May 12 '22 at 05:48
  • There is no "drag and drop" option here. Is this changed in newer versions? – Daniel Möller Nov 24 '23 at 02:45
  • @DanielMöller It's still there (but I can't guess which version you are using and if there is a bug or not in a very specific release) – JGH Nov 24 '23 at 11:10
  • I'm using 3.28.5-Firenze and couldn't find it at all. I resorted to the "Refactor fields" answer. – Daniel Möller Nov 24 '23 at 14:58
  • in 3.28 and above the "Drag and Drop designer" is part of "Layer Properties / Attribute Forms" - according to the interface the form layout can be arranged ... unfortunately dragging the fields order into a way I would like to have doesn't change anything in my forms – robert tuw Feb 14 '24 at 12:00
10

If I understand your question, you may use the "Refactor fields" algorithm from the Processing Toolbox (Ctrl+Alt+T): it allows manipulating the order of your fields stored in the Attribute Table.

Allows editing the structure of the attribute table of a vector layer.

Fields can be modified in their type and name, using a fields mapping.

The original layer is not modified. A new layer is generated, which contains a modified attribute table, according to the provided fields mapping.

Taras
  • 32,823
  • 4
  • 66
  • 137
mgri
  • 16,159
  • 6
  • 47
  • 80
  • 1
    it's exactly what i was looking for except that it creates a new layer with the new attributes order .. is there a way to do the same with the layer itself without creating a new one? – AsdAsd May 13 '17 at 11:55
  • @AbdullahAlaa, maybe not with common tools. Can't you overwrite the original layer? Maybe your task could be accomplished using some PyQGIS code. – mgri May 13 '17 at 12:04
5

This solution only changes the order of columns in the attribute table, and not in the original file.

This PyQGIS solution utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can achieve with the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None: """ Changes order of all columns in the layer Parameters: ========== :param layer_name: name of the layer :param new_order: a list with a new order of columns """

layer = QgsProject.instance().mapLayersByName(layer_name)[0]

layer_attr_table_config = layer.attributeTableConfig()
columns_config = layer_attr_table_config.columns()

columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                          column.type == 0 and column.name in new_order}
columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

layer_attr_table_config.setColumns(columns_new_order_list)
layer.setAttributeTableConfig(layer_attr_table_config)

return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result


References:

Taras
  • 32,823
  • 4
  • 66
  • 137
1

You may also use the "Table manager" plugin only available on QGIS 2.18 and > which only lets you edit the vector file without creating a new one.

Taras
  • 32,823
  • 4
  • 66
  • 137