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。

相关推荐
JckOLF04Z3 分钟前
C#客户端的异步操作
开发语言·c#
曹牧12 分钟前
C#:注释嵌套
开发语言·c#
中国搜索直付通16 分钟前
游戏车机端支付通道,会是下一个被低估的合规战场吗?
java·大数据·开发语言·人工智能·游戏
LccKyI21 分钟前
C# 零基础学习 Day01|基础概念梳理 + 思维导图总结
开发语言·笔记·学习·c#
AKA__Zas25 分钟前
芝士算法(位运算)
java·开发语言·算法·leetcode·学习方法
Livia要学习33 分钟前
Python三种常见高阶函数--map、filter、sorted
开发语言·python
奇树谦1 小时前
ZeroMQ 详解:轻量级高性能消息通信框架及 C++ 使用示例
开发语言·c++
不相心 -w-1 小时前
现代C++——智能指针
开发语言·c++
Ivanqhz1 小时前
php7 闭包实现
开发语言·后端·rust·php
weixin_440784119 小时前
【HandlerThread实现原理】
android·java·开发语言