当前位置:网站首页>QT displays the specified position and size of the picture
QT displays the specified position and size of the picture
2022-04-23 05:27:00 【Oriental forgetfulness】
Sometimes we need to cut the picture without cutting it , Display the area specified by the picture ,qt There are two ways to do this :
First use qss To achieve .
The second method uses drawing events to achieve .
Original picture :

Now we need to show its central position , But the length and width of the display area are different from the original image , The width is less than the width we want to display , The height should be greater than the height we show , At this time, you need to enlarge the picture according to the width scale , Then calculate according to the width proportion y The location of , from y Take the middle area .
qss Realization :
m_btn = new QPushButton(this);
m_btn->resize(392,220); // Display area
QPixmap pixmap("/home/yjd/yjd/test/MypixPos/2.jpg"); // Use QPixmap perhaps QImage To get the width and height of the picture .
int y = (pixmap.height()392.00/pixmap.width() - 220)/2pixmap.width()/392.00;// Calculation y The location of
QString pix = “border-image:url(/home/yjd/yjd/test/MypixPos/2.jpg)”;
QString pixstyle = QString("%1 %2 0 %3 0").arg(pix).arg(y).arg(y);//qss This way is to take the picture distance 、 Right 、 Next 、 Left position .
m_btn->setStyleSheet(pixstyle);

Draw event implementation :
QPainter p(this);
QPixmap pixmap("/home/yjd/yjd/test/MypixPos/2.jpg");
pixmap = pixmap.scaled(392,392.00/pixmap.width()*pixmap.height());// Enlarge in proportion to the width
p.drawPixmap(QRect(0,0,392,220),pixmap,QRect(0,(pixmap.height() - 220)/2,392,220));// first Rect Is the display area , the second Rect Is to take which area in the picture .

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_btn = new QPushButton(this);
m_btn->resize(392,220);
// m_btn->setStyleSheet("background-image: url(/home/yjd/yjd/test/MypixPos/1.jpg);"
// "background-repeat:no-repeat;"
// "background-position:center;"
// "background-origin:border;");
QPixmap pixmap("/home/yjd/yjd/test/MypixPos/2.jpg");
int y = (pixmap.height()*392.00/pixmap.width() - 220)/2*pixmap.width()/392.00;
qWarning()<<"------------"<<pixmap.height()<<pixmap.width()<<y;
QString pix = "border-image:url(/home/yjd/yjd/test/MypixPos/2.jpg)";
QString pixstyle = QString("%1 %2 0 %3 0").arg(pix).arg(y).arg(y);
m_btn->setStyleSheet(pixstyle);
m_lab = new QLabel(this);
m_lab->setGeometry(0,230,392,400*392.00/225);
m_lab->setStyleSheet("border-image:url(/home/yjd/yjd/test/MypixPos/2.jpg)");
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
QPixmap pixmap("/home/yjd/yjd/test/MypixPos/2.jpg");
pixmap = pixmap.scaled(392,392.00/pixmap.width()*pixmap.height());
p.drawPixmap(QRect(0,0,392,220),pixmap,QRect(0,(pixmap.height() - 220)/2,392,220));
}
版权声明
本文为[Oriental forgetfulness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220543307535.html
边栏推荐
- Data bus realizes the communication between brother components
- 如果我是pm之 演出电影vr购票展示
- How to add beautiful code blocks in word | a very complete method to sort out and compare
- Call the interface to get the time
- Create process memory management copy_ Mm - processes and threads (IX)
- The address value indicated by the pointer and the value of the object indicated by the pointer (learning notes)
- Create a tabbar component under the components folder, which is public
- Rog attack
- 2021-10-12
- 2021-09-27
猜你喜欢

What role do tools play in digital transformation?

Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)

Devops life cycle, all you want to know is here!

Kanban Quick Start Guide
![[untitled]](/img/49/770888f4f351f42af0e01c3a15ddfa.png)
[untitled]

C# ,类库

varnish入门

To understand Devops, you must read these ten books!
![Laravel [view]](/img/39/71db98d8832d9419bcc1097594d1b6.png)
Laravel [view]

Requirements for SQL server to retrieve SQL and user information
随机推荐
FileReader API file operation
弘玑|数字化时代下,HR如何进行自我变革和组织变革?
Requirements for SQL server to retrieve SQL and user information
!!!!!!!!!!!!!!!!!!
Uniapp wechat sharing
五一劳动节期间什么理财产品会有收益?
Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
Traversal array, object parent-child communication props / $emit
创建进程内存管理copy_mm - 进程与线程(九)
WTL self drawn control library (cqscheckcomboxbox)
WTL 自绘控件库 (CQsCheckComboxBox)
How to set the initial value of El input number to null
Blender programmed terrain production
Devops life cycle, all you want to know is here!
what is wifi6?
C test calls the paddlesharp module to recognize pictures and words
Low code and no code considerations
Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)
Study notes: unity customsrp-12-hdr