当前位置:网站首页>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
边栏推荐
- FE - Weex 使用简单封装数据加载插件为全局加载方法
- Sentinel规则持久化到Nacos
- Flask-Migrate 检测不到db.string() 等长度变化
- CUDA与Direct3D 一致性
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- Selenium memo: selenium\webdriver\remote\remote_ connection. Py:374: resourcewarning: unclosed < XXXX > solution
- Cglib agent - Code enhancement test
- eslint配置代码自动格式化
- unittest.TextTestRunner不生成txt测试报告
- Warp matrix functions in CUDA
猜你喜欢
Data science [viii]: SVD (I)
数据科学【九】:SVD(二)
Decryption skills of encrypted compressed files
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?
unittest.TextTestRunner不生成txt测试报告
In depth understanding of JUC concurrency (I) what is JUC
注解和反射详解以及运用
Win电脑截图黑屏解决办法
查询GPU时无进程运行,但是显存却被占用了
华为MindSpore开源实习机试题
随机推荐
Find the highest value of the current element Z-index of the page
Code skills - Controller Parameter annotation @requestparam
JS modification element attribute flipping commonly used in selenium's Web Automation
Warp shuffle in CUDA
CUDA中的Warp Shuffle
Flask-Migrate 检测不到db.string() 等长度变化
Storage space modifier in CUDA
The intern left a big hole when he ran away and made two online problems, which made me miserable
Distributed transactions: the final consistency scheme of reliable messages
automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
Codeforces Round #797 (Div. 3) A—E
奇葩pip install
阿里云MFA绑定Chrome浏览器
Summary of WLAN related knowledge points
pytest(1) 用例收集规则
Detailed definition of tensorrt data format
Eggjs -typeorm 之 TreeEntity 实战
The difference between session and cookies
FE - weex 开发 之 使用 weex-ui 组件与配置使用
selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法