当前位置:网站首页>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
边栏推荐
- 程序员的自我修养—找工作反思篇
- CUDA and Direct3D consistency
- Redis——大Key问题
- virtualenv和pipenv安装
- 链表(线性结构)
- automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
- Redis——缓存击穿、穿透、雪崩
- Pytest (1) case collection rules
- JS modification element attribute flipping commonly used in selenium's Web Automation
- Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
猜你喜欢
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
ShardingSphere-JDBC篇
Introduce two automatic code generators to help improve work efficiency
Win电脑截图黑屏解决办法
栈(线性结构)
Sublime Text 配置php编译环境
The intern left a big hole when he ran away and made two online problems, which made me miserable
广告业务Bug复盘总结
Warp shuffle in CUDA
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
随机推荐
实现strStr() II
阿里云MFA绑定Chrome浏览器
查询GPU时无进程运行,但是显存却被占用了
Sentinel规则持久化到Nacos
js中正则表达式的使用
web自动中利用win32上传附件
广告业务Bug复盘总结
Decryption skills of encrypted compressed files
Three suggestions for all students who have graduated and will graduate
Cglib代理-代码增强测试
Sparse array (nonlinear structure)
When requesting resttemplate, set the request header, request parameters, and request body.
CUDA user object
10 erreurs classiques de MySQL
Code skills - Controller Parameter annotation @requestparam
Redis - hot key issues
TensorRT的数据格式定义详解
TensorRT中的循环
automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
程序员的自我修养—找工作反思篇