吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 872|回复: 0
收起左侧

[C&C++ 原创] QOpenGLWidget显示QImage并实现缩放(纯代码版)

[复制链接]
zuoyizhongguo 发表于 2023-3-14 10:19
本帖最后由 zuoyizhongguo 于 2023-3-14 10:38 编辑

本贴使用QScrollArea+QOpenGLWidget 实现缩放,用于显示QImage。样式如图。

样式

样式

先自定义QOpenGLWidget ,然后自定义QWidget。
glwidget.h
[C++] 纯文本查看 复制代码
#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QOpenGLWidget>

class GLWidget : public QOpenGLWidget
{
    Q_OBJECT
public:
    explicit GLWidget(QWidget *parent = nullptr);

    void setImageData(const QImage& img);
protected:
    //重写事件
    void paintEvent(QPaintEvent* e) override;

private:
    QImage mImg;

};

#endif // GLWIDGET_H

glwidget.cpp
[C++] 纯文本查看 复制代码
#include "glwidget.h"
#include <QPainter>

GLWidget::GLWidget(QWidget *parent)
    : QOpenGLWidget{parent}
{
}

void GLWidget::setImageData(const QImage& img)
{
    if (!img.isNull())
    {
        mImg = img;
        update();
    }
}

void GLWidget::paintEvent(QPaintEvent* e)
{
    QPainter p;
    p.begin(this);
    p.drawImage(rect(), mImg);
    p.end();
}

viewwidget.h
[C++] 纯文本查看 复制代码
#ifndef VIEWWIDGET_H
#define VIEWWIDGET_H

#include <QWidget>
#include "glwidget.h"
#include <QScrollArea>

class ViewWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ViewWidget(QWidget *parent = nullptr);

signals:

public:
    GLWidget *glWidget;
    QScrollArea *scrollArea;

    float scaleFactor;

private slots:
    void buttonClicked(int id);

};

#endif // VIEWWIDGET_H

viewwidget.cpp
[C++] 纯文本查看 复制代码
#include "viewwidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QButtonGroup>

#pragma execution_character_set("utf-8")

ViewWidget::ViewWidget(QWidget *parent)
    : QWidget{parent}
    ,glWidget{new GLWidget}
    ,scrollArea{new QScrollArea}
{
//! [1]
    //上下布局,上方为QOpenGLWidget,下方为3个按钮(放大、缩小、适应窗口)
    QVBoxLayout *centralLayout=new QVBoxLayout;
    setLayout(centralLayout);
    centralLayout->setSpacing(0);
    centralLayout->setMargin(0);

    centralLayout->addWidget(scrollArea);
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(glWidget);

    //下方的3个按钮+1个弹簧,水平布局
    QHBoxLayout* toolboxLayout = new QHBoxLayout;
    centralLayout->addLayout(toolboxLayout);
    toolboxLayout->setSpacing(0);
    toolboxLayout->setMargin(0);

    QPushButton* buttonPlus = new QPushButton(QIcon("://icons/zoom_in.png"), QString(""), this);
    buttonPlus->setMaximumWidth(32);
    toolboxLayout->addWidget(buttonPlus);

    QPushButton* buttonMinus = new QPushButton(QIcon("://icons/zoom_out.png"), QString(""), this);
    buttonMinus->setMaximumWidth(32);
    toolboxLayout->addWidget(buttonMinus);

    QPushButton* buttonFit = new QPushButton(QIcon("://icons/zoom_fit.png"), QString(""), this);
    buttonFit->setMaximumWidth(32);
    toolboxLayout->addWidget(buttonFit);

    toolboxLayout->addStretch();

    //按钮组,方便多按钮调用一个函数
    QButtonGroup *toolbox=new QButtonGroup;
    toolbox->addButton(buttonPlus, 0);
    toolbox->addButton(buttonMinus, 1);
    toolbox->addButton(buttonFit, 2);
    connect(toolbox, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
//! [1]

    //初始化缩放系数
    scaleFactor = 1.0;
}

void ViewWidget::buttonClicked(int id)
{
    switch (id)
    {
    case 0: //plus
        scaleFactor *= 1.25;
        glWidget->setFixedSize(scrollArea->width() * scaleFactor, scrollArea->height() * scaleFactor);
        break;
    case 1: //minus
        scaleFactor *= 0.8;
        glWidget->setFixedSize(scrollArea->width() * scaleFactor, scrollArea->height() * scaleFactor);
        break;
    case 2: //fit
        scaleFactor = 1.0;
        glWidget->setMinimumSize(0, 0);
        glWidget->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
        glWidget->resized();
        break;
    default:
        break;
    }
}


【个人博客】夕西行 - 博客园 (cnblogs.com)

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 13:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表