当前位置:网站首页>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
边栏推荐
- 找到页面当前元素z-index最高的数值
- Redis——大Key问题
- Learn about various joins in SQL and their differences
- Three suggestions for all students who have graduated and will graduate
- 稀疏数组(非线性结构)
- virtualenv和pipenv安装
- CUDA中内置的Vector类型和变量
- CUDA user object
- Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
- Mech 3002 explanation
猜你喜欢

介绍两款代码自动生成器,帮助提升工作效率

Detailed definition of tensorrt data format

Sentinel rules persist to Nacos

Linked list (linear structure)

Idea announced a new default UI, which is too refreshing (including the application link)

Sublime Text 配置php编译环境

链表(线性结构)

数据科学【八】:SVD(一)

Cglib agent - Code enhancement test

自学table au
随机推荐
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
[self cultivation of programmers] - Reflection on job hunting Part II
In depth understanding of JUC concurrency (I) what is JUC
Detailed definition of tensorrt data format
日期时间API详解
CUDA中的异步数据拷贝
CUDA and Direct3D consistency
Three suggestions for all students who have graduated and will graduate
CUDA与Direct3D 一致性
Vector types and variables built in CUDA
Find the highest value of the current element Z-index of the page
Data science [viii]: SVD (I)
Sentinel rules persist to Nacos
Sudo right raising
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.)
阿里云MFA绑定Chrome浏览器
TensorRT的命令行程序
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?
CUDA中的线程层次
Warp shuffle in CUDA