Easy pit
Q: Why does the background style used QPalette::Window
or QPalette::Background
set QTextEdit
not take effect?
Answer: _UseQPalette::Base
instead. As follows:
QPalette pal = ui->textEdit->palette();
pal.setBrush(QPalette::Base, Qt::green);
ui->textEdit->setPalette(pal);
The operation effect is as follows:
Question: Why did the QTextEdit
background style not take effect after following the plan ?
Answer: There are many possible reasons, here are a few.
- The parent style covers the style of this control. At this time, you can set the background of this control to be automatically filled (not inherited from the parent class):
ui->textEdit->setAutoFillBackground(true);
- When setting the brush style, ColorRole is set incorrectly. Usually the search on the Internet is used
QPalette::Background
, but this method has been abandoned by Qt, and a new method should be adoptedQPalette::Base
. As follows:
QPalette pal = ui->textEdit->palette();
pal->setBrush(QPalette::Base, QPixmap("C:/1.jpg"));
Readers can search in the help documentation of Qt ColorRole
and find the following:
The roughly translated meaning is: QPalette::Background
Value has been deprecated, use QPalette::Window
instead.
However, after my experiment, I QTextEdit
use the type of child control:
QPalette pal = edit->palette();
pal.setBrush(QPalette:Window, Qt::red);
No effect!
As the description document of ColorRole said, most of the controls of the text entry use the QPalette::Base
method to set the style! And this method has been tested and proved to be feasible.
Program
Color palette
// 定义图片路径
const QString fileName = "C:/1.jpg";
QPalette pal = ui->textEdit->palette();
// 设置画刷,填充背景图片,且调整了图片大小
pal.setBrush(QPalette::Base, QPixmap(fileName).scaled(ui->textEdit->size()));
// 取消继承父类的背景样式
ui->textEdit->setAutoFillBackground(true);
// QTextEdit设置调色板,即填充了背景图片
ui->textEdit->setPalette(pal);
The effect is as follows:
You can also use this method to set the background color, you only need to pal.setBrush()
modify the parameters.
QPalette pal = ui->textEdit->palette();
// 设置画刷,填充背景颜色
pal.setBrush(QPalette::Base, Qt::red);
// 取消继承父类的背景样式
ui->textEdit->setAutoFillBackground(true);
// QTextEdit设置调色板,即填充了背景图片
ui->textEdit->setPalette(pal);
The operation effect is as follows:
Style sheet
QString fileName = "C:/1.jpg";
ui->textEdit->setStyleSheet("background-image: url(fileName)");
Hypertext Markup Language
QString fileName = "C:/1.jpg";
ui->textEdit->setHtml("<body background=" + fileName + "></body>");
to sum up
There are two problems in setting the background image using style sheets and hypertext markup language .
- The picture cannot be adapted to size;
- When the written text exceeds one screen, the edit box needs to turn the page, and you will find that the picture also turns the page.
I have not verified and explored these two questions, and readers can try them by themselves.
I personally recommend using the palette method, because it is implemented in C++ native language and is more versatile. And there is no such problem.