10

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?

1
  • 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
    Commented May 13, 2017 at 7:35

5 Answers 5

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

1
  • 2
    This does not save the changed order in the original files. Commented Nov 24, 2023 at 2:46
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

6
  • 1
    Is it possible to do this programmatically via PyQgis API? Commented May 12, 2022 at 5:41
  • 1
    Ok, found qgis.org/pyqgis/master/core/QgsEditFormConfig.html which responsible for that. Commented May 12, 2022 at 5:48
  • There is no "drag and drop" option here. Is this changed in newer versions? Commented Nov 24, 2023 at 2: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
    Commented Nov 24, 2023 at 11:10
  • 1
    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
    Commented Feb 14 at 12:00
11

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.

2
  • 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
    Commented May 13, 2017 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
    Commented May 13, 2017 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:

4
  • How can this be done without loading the source into the layer?
    – Er Re
    Commented Dec 30, 2022 at 16:13
  • You mean with PyQGIS or another Python package ?
    – Taras
    Commented Dec 30, 2022 at 19:23
  • in QGIS with python
    – Er Re
    Commented Dec 31, 2022 at 3:41
  • Possible, you just need to read your vector layer like described here: docs.qgis.org/latest/en/docs/pyqgis_developer_cookbook/…
    – Taras
    Commented Jan 2, 2023 at 6:39
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.