本帖最后由 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) |