QGIS – Editing Width of Drop Down Field List in Attribute Table

attribute-tabledropdownlistpyqgisqgiswidth

Can I change the width of the drop-down field (attribute) list in the attribute table that is used for editing, i.e. the highlighted drop-down in the image below with the value "ID"? The list is so narrow I can't see field names easily.
enter image description here

Best Answer

There is a PyQGIS solution I can suggest. Honestly, it was tricky, and not straightforward.

The thing you are looking for is the QFrame hidden under AttributeTable / mUpdateExpressionBox / mFieldCombo. It has a corresponding method for that called setFixedWidth().

Let's assume there is a point layer with an opened attribute table in edited mode, see the below:

input

The default width of this drop-down list is 100, possible to see by means of the width() method.

Proceed with Plugins > Python Console > Show Editor and paste the script below:

# imports
from qgis.utils import iface
from qgis.core import QgsProject
from PyQt5.QtWidgets import QApplication, QFrame

# accessing layer by its name
layer = QgsProject.instance().mapLayersByName("points_in_polygon")[0]

# looking for the attribute table widget
all_widgets = QApplication.instance().allWidgets()
attribute_table_widgets = [widget for widget in all_widgets if "AttributeTable" in widget.objectName() and layer.name() in widget.objectName()]

# if layer has no opened attribute table, open it
if len(attribute_table_widgets) == 0:
    iface.showAttributeTable(layer)
    all_widgets = QApplication.instance().allWidgets()
    attribute_table_widgets = [widget for widget in all_widgets if "AttributeTable" in widget.objectName() and layer.name() in widget.objectName()]

# finding attribute table widget that matches layer name 
for widget in attribute_table_widgets:
    # looping over elements of the layer's attribute table
    for elem1 in widget.children():
        if elem1.objectName() == 'mUpdateExpressionBox':
            for elem2 in elem1.children():
                if elem2.objectName() == 'mFieldCombo':
                    for elem3 in elem2.children():
                        if isinstance(elem3, QFrame):
                            # here change to desired width value
                            elem3.setFixedWidth(500)

Change the name of your input layer. Press Run script run script and get the output that will look like this:

result

Now the new width is 500.

This solution will work as long as you keep your attribute table open, does not matter if it will be edited multiple times.

Perhaps, there is more easier way to exist.

P.S. I would appreciate it if someone could try this code.


References:

Related Question