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。

相关推荐
Quz18 小时前
QML Hello World 入门示例
qt
xcyxiner4 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner4 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner5 天前
DicomViewer (添加模型类)3
qt
xcyxiner6 天前
DicomViewer (目录调整) 2
qt
xcyxiner6 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0067 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术7 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园7 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob7 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio