Qt 实现被鼠标悬浮的图片放大显示
2017-11-05 14:28
1901 查看
简述
界面上有三张图片,当鼠标指针移动到某图片之上,该图片会放大显示,当鼠标移到另一张图片之上时,前一张图片变为原大小,后一张图片放大显示。简述
效果图
源码
类的创建
头文件
源文件
界面文件
类的使用
源码下载
效果图
源码
类的创建
头文件
// 文件名 QtGuiApplication1.h #pragma once #include <QtWidgets/QMainWindow> #include "ui_QtGuiApplication1.h" #include <QImage> class QLabel; class QtGuiApplication1 : public QMainWindow { Q_OBJECT public: 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
相关文章推荐
- css实现鼠标悬停图片放大显示
- JS实现鼠标移动到缩略图显示大图的图片放大效果
- js实现将图片裁切成方形显示,鼠标移入放大效果
- JS实现鼠标移动到缩略图显示大图的图片放大效果
- css实现鼠标悬停图片放大显示
- jquery实现 鼠标悬浮 显示图片
- 浅谈CSS和JQuery实现鼠标悬浮图片放大效果
- Extjs实现在Ext.grid列鼠标悬浮显示图片
- CSS3实现图片鼠标悬浮放大效果
- 查看商品图片,鼠标悬浮图片放大js实现
- css 实现新闻列表鼠标悬浮时显示图片和内容简介
- Qt实现长图片的放大缩小以及动态显示
- JQuery实现鼠标移动到图片上显示边框效果
- 鼠标悬浮显示二级菜单效果的jquery实现
- JS实现鼠标移上图片显示、大图显示
- jquery实现 悬浮显示,例子显示图片
- qt程序实现多个图片连续显示效果像动画一样
- MFC中通过Tooltip类来实现悬浮鼠标显示提示信息
- js实现div跟谁鼠标悬浮移动显示
- Qt 显示图片 放大 缩小 移动