How to effectively set the background of QTextEdit

Easy pit

Q: Why does the background style used QPalette::Windowor QPalette::Backgroundset QTextEditnot take effect?

Answer: _UseQPalette::Baseinstead. As follows:

QPalette pal = ui->textEdit->palette();
pal.setBrush(QPalette::Base, Qt::green);
ui->textEdit->setPalette(pal);

The operation effect is as follows:
QTextEdit's color setting effect

Question: Why did the QTextEditbackground style not take effect after following the plan ?

Answer: There are many possible reasons, here are a few.

  1. 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);
  1. 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 adopted QPalette::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 ColorRoleand find the following:

ColorRole's official document description

The roughly translated meaning is: QPalette::BackgroundValue has been deprecated, use QPalette::Windowinstead.

However, after my experiment, I QTextEdituse 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::Basemethod 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:
Select a picture in the file dialog
running result

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:

running result

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 .

  1. The picture cannot be adapted to size;
  2. 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.

Guess you like

Origin blog.csdn.net/gkzscs/article/details/100537412