当前位置:网站首页>20210306 reprint how to make TextEdit have background pictures
20210306 reprint how to make TextEdit have background pictures
2022-07-02 06:35:00 【qq_ forty million nine hundred and thirty-eight thousand one hu】
One 、 to QTextEdit Add background image , There are two ways :
QTextEdit* iEdit = new QTextEdit();
1: Use style sheets :
iEdit->setStyleSheet("background-image:url(:/bmp/DSCN1604.JPG)");
Be careful : stay url() The first one in brackets “:” The colon must not be mistaken , Otherwise, it will not be displayed .
2: Use html
iEdit->setHtml("<body background=/"./bmp/DSCN1604.JPG/"> </body>");
Be careful : At this time, you don't need to use it like the above “:” Colon , Just use the relative path directly .
(Notice: Before using the above two methods , Don't forget to add pictures to qrc In the resource file )
//-------------------------------------------------------------------------------------------------------------------------------------------------
In fact, the above two methods have the same display effect , So it's almost the same , But they both have two problems :
Question 1 : The picture cannot be scaled to match the size of the edit box , After all, the road strength of the picture is used here , We can't scale it . So it's best to set the size of the edit box to the same size as the image before using !
Question two : This is more serious .
When we write more than one screen : The edit box needs to turn pages , You will find : The picture will also turn the page .
as follows : I put a background picture in the edit box :
I certainly hope this picture will always be fixed behind the edit box , Without flipping , But if I use the above method to set a background picture , When writing more than one screen : The picture will also flip the screen , Thus, the following situations may occur :
or : The picture will also be tiled many times behind the edit box , This is obviously not the effect we want .
The way to solve this problem is to put QTextEdit Set transparent , And then behind him widget Set the background picture on , So you OK 了
Set up QTextEdit Transparent approach
stay Qt All the problems in should be discussed in two systems , One is QWidget system , One is QGraphicsWidget system . This is no exception .
One : about QWidget In terms of system : That is to say, they all use QWidget And its derived classes . For this : or QTextEdit The parent object of is also QWidget Or its derived class .
So what we have to do is : Give Way QTextEdit Background becomes transparent , Then paint the picture at the position of the parent window behind it .
① Set up QTextEdit Transparent for the background :
QPalette pl = iEdit->palette();
pl.setBrush(QPalette::Base,QBrush(QColor(255,0,0,0)));
iEdit->setPalette(pl);
namely : Use a completely transparent brush to brush the background of the edit box !
And its parent window brushes the picture at this position , The key is to note whether the parent window is a top-level window ( Whether it has a parent window ), If so, be careful not to use setStyleSheet() To brush ( See the article for the reason http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/07/5565212.aspx).
Two : about QGraphicsWidget system , Then use the following method to set , Here I use code directly :
MainWindow::MainWindow(QWidget *parent)
: QGraphicsView(parent)
{
this->resize(360,640);
iScene = new QGraphicsScene(0,0,360,640);
iEdit = new QTextEdit();
iEdit->resize(360,400);
// The next paragraph is for my father view Brush picture
QPalette palette;
palette.setBrush(this->backgroundRole(),QBrush(QImage(":/bmp/dou.jpg")));
this->setPalette(palette);
// This section is responsible for setting the edit box item The background is transparent
palette.setBrush(QPalette::Base,QBrush(QColor(255,0,0,0)));
iEdit->setPalette(palette);
QGraphicsProxyWidget* widget = iScene->addWidget(iEdit);
palette.setBrush(QPalette::Window,QBrush(QColor(255,0,0,0)));
widget->setPalette(palette);
this->setScene(iScene);
}
so , There is still a big difference , And it seems that some places are difficult to understand , In fact, the key here involves two issues , One is QWidget System and QGraphicsWidget What is the difference between systems ? One is to use style sheets QPalette Set the background color / The difference between the two methods of pictures ( or :QPalette Of setBrush() The first argument to the function is QPalette::Base form still ptr->backgrounRole() The difference between ).
On these two questions , Please see
1:QWidget System and QGraphicsWidget The difference between systems
http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571149.aspx
2:Qt How to use style sheets in QPalette And relevant precautions
http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571187.aspx
This article is from CSDN Blog , Reprint please indicate the source :http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571088.aspx
Inherited address :https://blog.51cto.com/seanyxie/1376013
边栏推荐
- Flask-Migrate 检测不到db.string() 等长度变化
- 一起学习SQL中各种join以及它们的区别
- pytest(1) 用例收集规则
- Redis——热点key问题
- CUDA中的函数执行空间说明符
- Render minecraft scenes into real scenes using NVIDIA GPU
- MySql索引
- The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
- Pytest (1) case collection rules
- Self cultivation of programmers - Reflection on job hunting
猜你喜欢
随机推荐
virtualenv和pipenv安装
介绍两款代码自动生成器,帮助提升工作效率
Thread hierarchy in CUDA
奇葩pip install
TensorRT的功能
js中正则表达式的使用
Error "list" object is not callable in Web automatic switching window
构建学习tensorflow
FE - weex 开发 之 使用 weex-ui 组件与配置使用
Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
【程序员的自我修养]—找工作反思篇二
AWD学习
Log (common log framework)
pytest(3)parametrize参数化
Introduce two automatic code generators to help improve work efficiency
Functions of tensorrt
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
CUDA与Direct3D 一致性
递归(迷宫问题、8皇后问题)
记录一次RDS故障排除--RDS容量徒增








