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。

相关推荐
比昨天多敲两行34 分钟前
C++ 二叉搜索树
开发语言·c++·算法
Birdy_x1 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
海海不瞌睡(捏捏王子)1 小时前
C++ 知识点概要
开发语言·c++
桌面运维家2 小时前
VLAN配置进阶:抑制广播风暴,提升网络效率
开发语言·网络·php
一轮弯弯的明月3 小时前
Python基础-速通秘籍(下)
开发语言·笔记·python·学习
西西学代码3 小时前
Flutter---回调函数
开发语言·javascript·flutter
大尚来也3 小时前
深入HashMap底层:从JDK1.7到1.8的架构演进与性能突围
开发语言
森林里的程序猿猿4 小时前
并发设计模式
java·开发语言·jvm
222you4 小时前
四个主要的函数式接口
java·开发语言
smchaopiao5 小时前
Python中字典与列表合并的问题与解决方法
开发语言·python