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

原创粉丝点击