简体   繁体   中英

Qt QPlainTextEdit background

我想更改QPlainTextEdit的背景颜色,我该怎么做?

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.

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.

If QPlainTextEdit supports style sheets, you could do it like this:

myPlainTextEdit->setStyleSheet("background-color: yellow");

or

qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");

可能是你需要调用QPlainTextEdit::setBackgroundVisible(true)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM