How to change color of QTextEdit widget
Asked Answered
R

1

5

I'm trying to change the color of a QTextEdit to black, regardless if there's text or none, to give it a terminal look. It seems to me that the default white background color of QTextEdit (PyQy4) can't be changed by means that are otherwise working for other Qt widgets. I have tried the following:

w.setTextBackgroundColor(QColor(0,0,0))

w.setAutoFillBackground(True)

p = w.palette()
p.setColor(w.backgroundRole(), QColor(0,0,0))
w.setPalette(p)
Respectable answered 21/11, 2017 at 2:42 Comment(0)
H
7

A simple solution is using qss:

w.setStyleSheet("background-color: rgb(0, 0, 0);")

If you want to use QPalette you should apply it to the viewport():

p = w.viewport().palette()
p.setColor(w.viewport().backgroundRole(), QtGui.QColor(0,0,0))
wt.viewport().setPalette(p)
Hexamethylenetetramine answered 21/11, 2017 at 2:49 Comment(3)
Thank you. viewport() worked. setStyleSheet() didn't tho.Respectable
@Respectable It works for me both. Maybe the style you use is blocking that property. :PHexamethylenetetramine
Both options work for me, though with the stylesheet method the scrollbar is also affected (which looked weird for me).Pokeweed

© 2022 - 2024 — McMap. All rights reserved.