Qt

您所在的位置:网站首页 图片的移动 Qt

Qt

2024-07-04 04:41:22| 来源: 网络整理| 查看: 265

用的Qt的中的的的paintEvent绘图,通过坐标系的变换来实现对图像的位置变化,通过滚轮事件对图像的大小进行缩放具体代码如下:

新建Qt Widget应用程序

新增头文件:

#include #include #include #include #include #include

mainwindow.h中这样写:

class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent *ev); void mouseMoveEvent(QMouseEvent *ev); void wheelEvent(QWheelEvent *ev); void zoomPixmap(); private: Ui::MainWindow *ui; QRect rect; float zoom;//缩放系数 QPoint CurrentCoordinates,LastCoordinates;//当前的坐标,上一次移动的坐标 };

mainwindow.cpp中这样写:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //初始化矩形的尺寸和放大缩小的系数 rect.setRect(50,50,50,50); zoom = 1.0; } MainWindow::~MainWindow() { delete ui; } //绘图 void MainWindow::paintEvent(QPaintEvent *event) { QPainter p(this); QBrush brush; QPen pen; //设置坐标系 p.translate(CurrentCoordinates); //反锯齿 p.setRenderHint(QPainter::Antialiasing); //设置画笔颜色,边框的颜色 brush.setColor(Qt::blue); //设置填充的风格,不设置默认不填充 brush.setStyle(Qt::SolidPattern); //设置画刷的颜色,矩形填充的颜色 pen.setColor(Qt::red); //将画笔和画刷给QPainter p.setPen(pen); p.setBrush(brush); //画矩形 p.drawRect(rect); } //放大缩小函数 void MainWindow::zoomPixmap() { float width,hight; width = rect.width(); hight = rect.height(); width = width*zoom; hight = hight*zoom; rect.setWidth(width); rect.setHeight(hight); } //鼠标按键按下事件 void MainWindow::mousePressEvent(QMouseEvent *ev) { //鼠标按下时记录当前按下的坐标 if(ev->button() == Qt::LeftButton) { if(ev->x()>rect.x() && ev->x()y()>rect.y() && ev->y()pos(); } } } } //鼠标滚轮事件 void MainWindow::wheelEvent(QWheelEvent *ev) { //如果滚轮向上滚动就放大尺寸 if(ev->delta() > 0) { zoom = 1.1; } else//如果滚轮向下滚动就缩小尺寸 { zoom = 0.9; } zoomPixmap(); update(); } //鼠标移动事件 void MainWindow::mouseMoveEvent(QMouseEvent *ev) { //当鼠标左键按下且移动 if(ev->buttons() & Qt::LeftButton){ //计算当前需要移动的距离,保存本次的坐标 CurrentCoordinates = CurrentCoordinates - LastCoordinates + ev->pos(); LastCoordinates = ev->pos(); ev->accept(); this->update(); } }

实现效果:

       



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭