4

At 3.34, the color of selected features in the main map can be changed in either the Settings > Options menu or in the Project > Properties.

However, I have not found a way to change the color of selected records in the attribute table. They always display as blue, reverse-video.

I would like to make the selection color the same in both the map window and the attribute table window. For example, if a selected map feature displayed in yellow, then the selected table records would have the same yellow background, without the need for reverse-video.

An internet search turned up nothing. So... how to make the selection color the same in the map and attribute table windows?

0

1 Answer 1

5

The blue selection background color of the attribute table rows is the default style of the QTableView widget in the Qt GUI library on which QGIS is built.

Maybe there is a better way, but here is a slightly hacky but effective method to change it on a per-project basis by using a project Python macro and setting a style sheet on the QTableView when the attribute table dialog is opened. The current canvas selection color is retrieved and used as the background color in the style sheet.

Note that if the selection color is yellow, the default white font color of selected items is very difficult to see so this solution also changes that to black.

Also note that, in it's current form, this works for one open attribute table at a time. If an additional attribute table is opened (i.e. you have two tables open at the same time) the one opened second will not be affected. This behavior could be modified fairly easily if required.

*I have tested this on Windows and Ubuntu and it works on both of those OS's. Not sure about mac.

Under the Project menu, select Properties and open the Macros tab. First you need to check the Python macros checkbox, then paste in the code block below and click OK.

from qgis.core import QgsApplication
from qgis.utils import iface
from qgis.PyQt.QtWidgets import QTableView

def windowFocusChanged(off, on):
    tbl_dlgs = [w for w in app.allWidgets() if 'QgsAttributeTableDialog' in w.objectName()]
    if tbl_dlgs:
        tbl_dlg = tbl_dlgs[0]
        tbl_view = tbl_dlg.findChild(QTableView)
        selection_color = iface.mapCanvas().selectionColor().rgba64()
        r_val = selection_color.red8()
        g_val = selection_color.green8()
        b_val = selection_color.blue8()
        tbl_view.setStyleSheet(f"selection-background-color: rgb({r_val}, {g_val}, {b_val});\
                                selection-color: rgb(0, 0, 0);")

def openProject():
    global app
    app = QgsApplication.instance()
    app.focusChanged.connect(windowFocusChanged)

def saveProject():
    pass

def closeProject():
    app.focusChanged.disconnect(windowFocusChanged)

See screenshot below:

enter image description here

You will then need to reload the project for the macro to take effect.

Results below:

enter image description here

Note that whenever you open the project you will be prompted to enable macros via the message bar:

enter image description here

You can avoid this by enabling macros for the current session under Settings>>Options>>General>>Project Files.

enter image description here

Or you could also do this with pyqgis via a startup.py file.

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.