Qt之图片的放大与缩小

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include <QDebug>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private:
    void resize_label();
private slots:
    void on_doubleSpinBox_h_valueChanged(double arg1);

    void on_doubleSpinBox_v_valueChanged(double arg1);

private:
    Ui::Widget *ui;
    QLabel label;
    QImage image;
    QImage original_image;
    QString path;
};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    path=QApplication::applicationDirPath()+"/1.png";
    qDebug()<<path;

    original_image=QImage(path);
    image=original_image;
    label.setPixmap(QPixmap::fromImage(image));

    label.show();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::resize_label()
{
    QMatrix matrix;
    qDebug()<<"h:"<<ui->doubleSpinBox_h->value();
    matrix.scale(ui->doubleSpinBox_h->value(),ui->doubleSpinBox_v->value());
    image=original_image.transformed(matrix);
    label.setPixmap(QPixmap::fromImage(image));
    label.resize(image.size());
}

void Widget::on_doubleSpinBox_h_valueChanged(double arg1)
{
    resize_label();
}

void Widget::on_doubleSpinBox_v_valueChanged(double arg1)
{
    resize_label();
}

效果:

如何显示一张图片:

1.png--->QImage--->QPixmap--->QLabel

改变图片的水平大小和垂直大小,其实是在改变QImage。

相关推荐
JANGHIGH5 分钟前
c++ 多线程(三)
开发语言·c++
2503_928411567 分钟前
12.9 Vue3+Vuex+Js+El-Plus+vite(项目搭建)
开发语言·javascript·ecmascript
卓码软件测评13 分钟前
第三方软件验收评测机构【Gatling安装指南:Java环境配置和IDE插件安装】
java·开发语言·ide·测试工具·负载均衡
weixin_3077791319 分钟前
Jenkins中的Jakarta Activation API插件:功能、使用与最佳实践
运维·开发语言·ci/cd·自动化·jenkins
weixin_4399306438 分钟前
前端js日期计算跨月导致的错误
开发语言·前端·javascript
liu****1 小时前
9.二叉树(一)
c语言·开发语言·数据结构·算法·链表
她说..1 小时前
Spring AOP场景2——数据脱敏(附带源码)
java·开发语言·java-ee·springboot·spring aop
sunly_1 小时前
Flutter:showModalBottomSheet底部弹出完整页面
开发语言·javascript·flutter
漏洞文库-Web安全2 小时前
AWD比赛随笔
开发语言·python·安全·web安全·网络安全·ctf·awd
张人玉2 小时前
C#通信精讲系列——C# 通讯编程基础(含代码实例)
开发语言·c#·c#通信