I want to change the background color of a QPlainTextEdit
, how do I do this?
Asked
Active
Viewed 9,956 times
5 Answers
15
Modify the palette of your plain text edit. Sample program:
#include <QApplication>
#include <QPlainTextEdit>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPlainTextEdit edit;
QPalette p = edit.palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::red);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::red);
edit.setPalette(p);
edit.show();
return app.exec();
}
Substitute whatever color you want, of course.

Bill
- 14,257
- 4
- 43
- 55
-
1Note that with this method, applying style sheets to a parent or the control itself will disable this palette. Had some fun troubleshooting this just now :) – RandomInsano Aug 14 '12 at 22:26
-
Good to know, thanks! I haven't worked with style sheets yet, so thanks for the advance notice. – Bill Aug 15 '12 at 14:39
4
If QPlainTextEdit supports style sheets, you could do it like this:
myPlainTextEdit->setStyleSheet("background-color: yellow");
or
qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");

Sam Dutton
- 14,775
- 6
- 54
- 64
-
Note that this also affects the scrollbar color which is probably not what you want. – letmaik Jun 08 '18 at 18:10
3
Slightly confusingly they call it role rather than colour/color.
https://doc.qt.io/qt-5/qwidget.html#setBackgroundRole
hint - if you can't find a function for a particular control, click on show inherited members - most general settings are in qWidget which is the basis for eveything drawn on screen.

Vaidotas Strazdas
- 686
- 3
- 14

Martin Beckett
- 94,801
- 28
- 188
- 263
-
Haven't used it but see this thread http://lists.trolltech.com/qt-interest/2006-07/thread00174-0.html – Martin Beckett Oct 07 '09 at 00:12
0
In order to modify the background, you need to modify the palette of your QPlainTextEdit and to set background visible:
myPlainTextEdit->setPalette(QPalette(/*Select the constructor you need*/));
myPlainTextEdit->setBackgroundVisible(true);

Vaidotas Strazdas
- 686
- 3
- 14

Patrice Bernassola
- 14,136
- 6
- 46
- 59