How do I get current background color for QlineEdit widget in Pyqt5?

Question:

How do I get the background color of a QlineEdit widget in pyqt5?

Asked By: ScottCov

||

Answers:

To complete my comment, this regex "(?<=background-color:)(.*?)(?=[;}])" can do the job for simple cases (see simulation I did here: https://regex101.com/r/SEB9hQ/1). You can improve it to fit your needs.
The captured group should be strip()ed to get rid of spaces.

Answered By: zigma12

By default, QLineEdit uses the Base palette role for its background and also uses that role for its backgroundRole().

Since basic properties of stylesheets set some color roles for the local widget palette[1], the solution is to use that along with the role:

background = label.palette().color(label.backgroundRole())
print(background.name())

[1] See this answer to know how QSS colors can alter a widget palette.

Answered By: musicamante
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.