【发布时间】:2009-10-06 23:36:18
【问题描述】:
我想更改QPlainTextEdit 的背景颜色,我该怎么做?
【问题讨论】:
我想更改QPlainTextEdit 的背景颜色,我该怎么做?
【问题讨论】:
修改纯文本编辑的调色板。示例程序:
#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();
}
当然可以替换任何你想要的颜色。
【讨论】:
如果 QPlainTextEdit 支持样式表,你可以这样做:
myPlainTextEdit->setStyleSheet("background-color: yellow");
或
qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");
【讨论】:
他们称之为角色而不是颜色/颜色,有点令人困惑。
https://doc.qt.io/qt-5/qwidget.html#setBackgroundRole
提示 - 如果您找不到特定控件的功能,请单击显示继承的成员 - 大多数常规设置都在 qWidget 中,这是在屏幕上绘制的一切的基础。
【讨论】:
您可能需要致电QPlainTextEdit::setBackgroundVisible(true)。
【讨论】:
为了修改背景,您需要修改 QPlainTextEdit 的palette 并设置背景可见:
myPlainTextEdit->setPalette(QPalette(/*Select the constructor you need*/));
myPlainTextEdit->setBackgroundVisible(true);
【讨论】: