当前位置:网站首页>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
边栏推荐
猜你喜欢
2021-10-12
如果我是pm之 演出电影vr购票展示
How to add beautiful code blocks in word | a very complete method to sort out and compare
On the use of constant pointer and pointer constant -- exercise (record)
Uniapp wechat sharing
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
Laravel [view]
Uncle wolf is looking for a translator -- Plato -- ongoing translation
C test calls the paddlesharp module to recognize pictures and words
Project manager's thinking mode worth trying: project success equation
随机推荐
Requirements for SQL server to retrieve SQL and user information
What are the most popular recruitment technical skills in 2022? You can't think of it
2021-11-08
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
Interpretation of common SQL statements
Source code analysis of how to use jump table in redis
What role do tools play in digital transformation?
Uncle wolf is looking for a translator -- Plato -- ongoing translation
Quick app bottom navigation bar
If I am PM's performance, movie VR ticket purchase display
Why can't V-IF and V-for be used together
Cross domain CORS relationship~
Low code and no code considerations
Redis的基本知识
狼叔来找翻译人员了--plato--持续翻译中.....
egg的static的前缀是可以修改惹,靴靴
Understand the relationship between promise async await
2021-10-08
Qingdao agile tour, coming!