Qt 实现被鼠标悬浮的图片放大显示
来源:互联网 发布:wamp怎么运行php 编辑:程序博客网 时间:2024/05/16 18:56
简述
界面上有三张图片,当鼠标指针移动到某图片之上,该图片会放大显示,当鼠标移到另一张图片之上时,前一张图片变为原大小,后一张图片放大显示。
- 简述
- 效果图
- 源码
- 类的创建
- 头文件
- 源文件
- 界面文件
- 类的使用
- 类的创建
- 源码下载
效果图
源码
类的创建
头文件
// 文件名 QtGuiApplication1.h#pragma once#include <QtWidgets/QMainWindow>#include "ui_QtGuiApplication1.h"#include <QImage>class QLabel;class QtGuiApplication1 : public QMainWindow{ Q_OBJECTpublic: QtGuiApplication1(QWidget *parent = Q_NULLPTR);protected: // Event handlers virtual void mousePressEvent(QMouseEvent *event); virtual void mouseReleaseEvent(QMouseEvent *event); virtual void mouseDoubleClickEvent(QMouseEvent *event); virtual void mouseMoveEvent(QMouseEvent *event);private: Ui::QtGuiApplication1Class ui; QLabel *mouseStatus; QImage *img1; QImage *img2; QImage *img3; bool scaleEnable1; bool scaleEnable2; bool scaleEnable3;};
源文件
// 文件名 QtGuiApplication1.cpp#include "QtGuiApplication1.h"#include <QMouseEvent>#include <QLabel>#include <QPixmap>#include <QMatrix>QtGuiApplication1::QtGuiApplication1(QWidget *parent) : QMainWindow(parent){ ui.setupUi(this); setWindowTitle(QStringLiteral("鼠标事件")); centralWidget()->setMouseTracking(true); setMouseTracking(true); ui.label_1->setMouseTracking(true); ui.label_2->setMouseTracking(true); ui.label_3->setMouseTracking(true); mouseStatus = new QLabel; statusBar()->addPermanentWidget(mouseStatus); img1 = new QImage; img2 = new QImage; img3 = new QImage; img1->load(":/goal/resource/KuiBeiKe.jpg"); img2->load(":/goal/resource/treeRoad.jpg"); img3->load(":/goal/resource/KuiBeiKe.jpg"); ui.label_1->setPixmap(QPixmap::fromImage(*img1)); ui.label_1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.label_2->setPixmap(QPixmap::fromImage(*img2)); ui.label_2->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.label_3->setPixmap(QPixmap::fromImage(*img3)); ui.label_3->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); scaleEnable1 = true; scaleEnable2 = true; scaleEnable3 = true;}void QtGuiApplication1::mousePressEvent(QMouseEvent *event){ QString str = "X:" + QString::number(event->x()) + " Y:" + QString::number(event->y()); switch (event->button()) { case Qt::LeftButton: str = QStringLiteral("左键: ") + str; break; case Qt::RightButton: str = QStringLiteral("右键: ") + str; break; case Qt::MidButton: str = QStringLiteral("中键: ") + str; break; default: break; } mouseStatus->setText(str);}void QtGuiApplication1::mouseReleaseEvent(QMouseEvent *event){}void QtGuiApplication1::mouseDoubleClickEvent(QMouseEvent *event){}void QtGuiApplication1::mouseMoveEvent(QMouseEvent *event){ QString str = "X:" + QString::number(event->x()) + " Y:" + QString::number(event->y()); statusBar()->showMessage(str); int x = event->x(); if (x > 70 && x<124) { QMatrix matrix; matrix.scale(1, 1); if (scaleEnable1) { ui.label_1->setFixedHeight(250); ui.label_1->setFixedWidth(157); scaleEnable1 = false; } if (!scaleEnable2) { ui.label_2->setFixedHeight(54); ui.label_2->setFixedWidth(61); scaleEnable2 = true; } if (!scaleEnable3) { ui.label_3->setFixedHeight(54); ui.label_3->setFixedWidth(61); scaleEnable3 = true; } } else if (x > 270 && x<324) { if (scaleEnable2) { ui.label_2->setFixedHeight(250); ui.label_2->setFixedWidth(157); scaleEnable2 = false; } if (!scaleEnable1) { ui.label_1->setFixedHeight(54); ui.label_1->setFixedWidth(61); scaleEnable1 = true; } if (!scaleEnable3) { ui.label_3->setFixedHeight(54); ui.label_3->setFixedWidth(61); scaleEnable3 = true; } } else if (x > 430 && x < 484) { if (scaleEnable3) { ui.label_3->setFixedHeight(250); ui.label_3->setFixedWidth(157); scaleEnable3 = false; } if (!scaleEnable1) { ui.label_1->setFixedHeight(54); ui.label_1->setFixedWidth(61); scaleEnable1 = true; } if (!scaleEnable2) { ui.label_2->setFixedHeight(54); ui.label_2->setFixedWidth(61); scaleEnable2 = true; } }}
界面文件
类的使用
main.cpp
#include "QtGuiApplication1.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); QtGuiApplication1 w; w.show(); return a.exec();}
源码下载
【站内链接】
http://download.csdn.net/download/qq_35488967/10104760
阅读全文
0 0
- Qt 实现被鼠标悬浮的图片放大显示
- 鼠标悬浮图片放大
- 查看商品图片,鼠标悬浮图片放大js实现
- 如何实现鼠标悬浮放大
- jquery实现 鼠标悬浮 显示图片
- 鼠标悬浮,图片跟随鼠标放大
- css实现鼠标悬停图片放大显示
- css实现鼠标悬停图片放大显示
- Qt实现长图片的放大缩小以及动态显示
- 如何实现鼠标悬浮上去后放大
- Extjs实现在Ext.grid列鼠标悬浮显示图片
- JS实现放大镜功能,鼠标经过一个图片,放大显示
- 鼠标移动到图片上实现图片的放大缩小
- JS 实现鼠标指向图片时图片放大的效果
- 图片的鼠标悬浮效果
- 点击图片显示放大的图片,并悬浮于窗体之上
- html+css鼠标悬浮显示图片
- 鼠标悬浮实现各种图片散开的特效
- JavaGUI游戏准备----工具类的创建
- Socket编程-TCP非阻塞方式
- 第三章
- 多线程I
- 与别校的联hu考zuo(11.4)
- Qt 实现被鼠标悬浮的图片放大显示
- 设计模式(19)——观察者 Observer
- 排序算法总结
- JAVA字符串的操作
- 多线程II----thread类中的方法
- caffe的prototxt绘制
- Python编程之四书五经
- Zookeeper的局限性
- 【软考】之面向对象技术