'How to change the selected background color of a text pyqt5

I want to change the background color of the selection:

i want to change the background color of the selection

Someone can help me ?



Solution 1:[1]

Using QPalette

Qt uses the QPalette as basis for drawing all its widgets; there are various roles to which colors (or, better, brushes) can be assigned, including the Highlight role used to draw the background of selected items and text.

You can get the global application palette (QApplication.palette()) or that of the widget, and set the color for each role; note that changing the palette has no direct effect on the widget, and it must be set back on the widget in order to take effect.

lineEdit = QLineEdit()
palette = lineEdit.palette()
palette.setColor(QPalette.Highlight, QColor('aqua'))
lineEdit.setPalette(palette)

You can make this global for all widgets; note that this will change the highlight color for any widget that supports selections, including item views:

palette = QPalette()
palette.setColor(QPalette.Highlight, QColor('aqua'))
QApplication.setPalette(palette)

Using Qt Style Sheets

Alternatively, Qt provides a custom implementation of style sheets (based on the CSS v2.1 specification):

lineEdit.setStyleSheet('selection-background-color: aqua;')

Similarly to the above, this can also be set globally:

QApplication.instance().setStyleSheet('selection-background-color: aqua;')

Note that it's generally discouraged to set global properties in the stylesheet of the application as much as for containers (QWidget, QFrame, etc) and complex widgets like item views; selectors should always be preferred, and this allows more control over what widgets (or widget types) will be "styled". The following sets the selection background only for QLineEdit widgets (including subclasses):

QApplication.instance().setStyleSheet('''
    QLineEdit {
        selection-background-color: aqua;
    }
''')

Important notes about altering default colors

The text color should also be set, as your background might be incompatible with the default colors of the system, thus making the selected text difficult (or impossible) to read. You must then find a color that has enough contrast with the background and also ensures good readability: for instance, a green background should not have a red text.

The role for the palette is HighlightedText:

palette.setColor(QPalette.Highlight, QColor('aqua'))
palette.setColor(QPalette.HighlightedText, QColor('darkblue'))

The property of style sheets is selection-color:

QApplication.instance().setStyleSheet('''
    QLineEdit {
        selection-background-color: aqua;
        selection-color: darkblue;
    }
''')

You can use this online tool to check color combinations and see if they are good enough.

Finally, you must also always consider the default Base palette color, which is the color used as background of widgets that accept user input (text, numbers, item views, etc.), otherwise you might choose a color that will make it impossible to distinguish the selection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 musicamante