最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT如何通過鼠標(biāo)事件實(shí)現(xiàn)圖片的拖動(dòng)和縮放

 更新時(shí)間:2024年10月11日 09:30:06   作者:笑非不退  
本文介紹了如何通過鼠標(biāo)拖動(dòng)移動(dòng)圖片以及使用鼠標(biāo)滾輪進(jìn)行圖片縮放的技術(shù)實(shí)現(xiàn),包括完整的解決方案,ImageWidget.h、ImageWidget.cpp和main.cpp的編寫,以及詳細(xì)的函數(shù)解釋,如paintEvent()重繪圖片,以及平滑縮放和偏移量的應(yīng)用等,需要的朋友可以參考下

通過鼠標(biāo)拖動(dòng)來移動(dòng)圖片,并使用鼠標(biāo)滾輪來縮放圖片。

1、實(shí)現(xiàn)步驟:

1、移動(dòng)圖片:

使用QPoint記錄圖片的偏移量,當(dāng)鼠標(biāo)拖動(dòng)時(shí)更新這個(gè)偏移量,在paintEvent()中根據(jù)偏移量繪制圖片。

2、縮放圖片:

使用滾輪事件調(diào)整圖片的縮放倍數(shù),在paintEvent()中重新繪制圖片時(shí)應(yīng)用縮放。

2、完整的解決方案:

1、ImageWidget.h

#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QPainter>
#include <QVBoxLayout>
#include <QDebug>

class ImageWidget : public QWidget
{
    Q_OBJECT

public:
    ImageWidget(QWidget *parent = nullptr);
protected:
    // 重寫paintEvent,用于繪制圖片
    void paintEvent(QPaintEvent *event) override;

    // 鼠標(biāo)按下事件
    void mousePressEvent(QMouseEvent *event) override;

    // 鼠標(biāo)移動(dòng)事件
    void mouseMoveEvent(QMouseEvent *event) override;
    // 鼠標(biāo)釋放事件
    void mouseReleaseEvent(QMouseEvent *event) override;
    // 滾輪事件,用于縮放圖片
    void wheelEvent(QWheelEvent *event) override;

private:
    QPixmap pixmap;            // 圖片
    QPoint lastMousePosition;  // 上一次鼠標(biāo)點(diǎn)擊的位置
    QPoint offset;             // 圖片的偏移量
    bool isDragging;           // 標(biāo)記是否正在拖動(dòng)圖片
    double scaleFactor;        // 縮放倍數(shù)
};

#endif // IMAGEWIDGET_H

2、ImageWidget.cpp

#include "imagewidget.h"

ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent)
{
    // 加載圖片
    pixmap = QPixmap(R"(C:\Users\LiangQL\Desktop\開發(fā)者基礎(chǔ)認(rèn)證.jpg)");  // 替換成你的圖片路徑
    scaleFactor = 1.0;        // 初始縮放因子為1
    isDragging = false;       // 初始狀態(tài)下不拖動(dòng)
    offset = QPoint(0, 0);    // 圖片的初始偏移為(0, 0)
}
// 重寫paintEvent,用于繪制圖片
void ImageWidget::paintEvent(QPaintEvent *event)
{
    // 創(chuàng)建一個(gè)QPainter,用于繪制圖片
    QPainter painter(this);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);  // 開啟平滑縮放

    // 計(jì)算縮放后的圖片尺寸
    QSize scaledSize = pixmap.size() * scaleFactor;

    // 繪制圖片,應(yīng)用縮放和偏移量
    painter.drawPixmap(offset, pixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

// 鼠標(biāo)按下事件
void ImageWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = true;                // 開始拖動(dòng)
        lastMousePosition = event->pos(); // 記錄鼠標(biāo)點(diǎn)擊位置
    }
}

// 鼠標(biāo)移動(dòng)事件
void ImageWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isDragging) {
        // 計(jì)算鼠標(biāo)的移動(dòng)距離
        QPoint delta = event->pos() - lastMousePosition;

        // 更新圖片的偏移量
        offset += delta;

        // 更新鼠標(biāo)位置
        lastMousePosition = event->pos();

        // 觸發(fā)重新繪制
        update();
    }
}

// 鼠標(biāo)釋放事件
void ImageWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = false;  // 結(jié)束拖動(dòng)
    }
}

// 滾輪事件,用于縮放圖片
void ImageWidget::wheelEvent(QWheelEvent *event)
{
    // 滾輪向上放大,向下縮小
    if (event->angleDelta().y() > 0) {
        scaleFactor *= 1.1;  // 放大
    } else {
        scaleFactor /= 1.1;  // 縮小
    }

    // 限制縮放范圍
    scaleFactor = qBound(0.1, scaleFactor, 10.0);

    // 更新顯示
    update();
}

3、 調(diào)用main.cpp

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QPainter>
#include <QVBoxLayout>
#include <QDebug>
#include "ImageWidget.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 創(chuàng)建主窗口
    QWidget window;
    window.setFixedSize(800, 600);  // 設(shè)置窗口大小

    // 創(chuàng)建圖片控件(自定義的類)
    ImageWidget *imageWidget = new ImageWidget;

    // 使用布局管理器將圖片控件放置在窗口中
    QVBoxLayout *layout = new QVBoxLayout(&window);
    layout->addWidget(imageWidget);

    // 顯示主窗口
    window.show();

    return app.exec();
}

3、詳細(xì)解釋:

1. paintEvent() 重繪圖片:

paintEvent()方法用于自定義控件的繪制。在這個(gè)方法中,我們使用QPainter對(duì)象來繪制圖片。
pixmap.scaled():使用pixmap.scaled()方法來縮放圖片,根據(jù)scaleFactor縮放圖片并保持其寬高比。
painter.drawPixmap(offset, ...):在指定的偏移量位置繪制圖片,offset表示圖片在控件中的偏移。

2. 圖片移動(dòng):

mousePressEvent():當(dāng)鼠標(biāo)左鍵按下時(shí),記錄鼠標(biāo)點(diǎn)擊位置,并開始拖動(dòng)圖片(isDragging = true)。
mouseMoveEvent():如果正在拖動(dòng)圖片(isDragging為true),則計(jì)算鼠標(biāo)的移動(dòng)距離,將圖片的offset偏移量更新為原來的偏移量加上鼠標(biāo)移動(dòng)的距離。
mouseReleaseEvent():當(dāng)鼠標(biāo)左鍵釋放時(shí),結(jié)束拖動(dòng)(isDragging = false)。

3. 圖片縮放:

wheelEvent():處理鼠標(biāo)滾輪事件。滾輪向上時(shí),放大圖片;滾輪向下時(shí),縮小圖片。
scaleFactor *= 1.1:每次滾動(dòng)時(shí),縮放倍數(shù)按10%變化。
qBound(0.1, scaleFactor, 10.0):限制縮放比例在0.1到10倍之間,防止圖片縮得太小或放得太大。

4. 平滑縮放:

Qt::SmoothTransformation:使用平滑縮放算法,防止圖片縮放后出現(xiàn)鋸齒。

5. 偏移量的作用:

offset用于記錄圖片相對(duì)于窗口的偏移量,當(dāng)鼠標(biāo)拖動(dòng)時(shí),更新這個(gè)偏移量。
在paintEvent()中,每次繪制時(shí)都使用這個(gè)偏移量來控制圖片的位置。
運(yùn)行效果:
拖動(dòng)圖片:按住鼠標(biāo)左鍵并移動(dòng),可以看到圖片在窗口內(nèi)部移動(dòng)。
縮放圖片:滾動(dòng)鼠標(biāo)滾輪,圖片會(huì)放大或縮小,同時(shí)保持寬高比。

總結(jié)

到此這篇關(guān)于QT如何通過鼠標(biāo)事件實(shí)現(xiàn)圖片的拖動(dòng)和縮放的文章就介紹到這了,更多相關(guān)QT鼠標(biāo)事件實(shí)現(xiàn)圖片拖動(dòng)縮放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

栖霞市| 双城市| 交口县| 沧源| 垦利县| 甘德县| 黄山市| 泸西县| 时尚| 云龙县| 留坝县| 湘西| 白城市| 金华市| 仙游县| 浙江省| 太仓市| 通渭县| 昌都县| 宣化县| 伽师县| 汉阴县| 申扎县| 唐河县| 怀远县| 大同县| 涞水县| 额尔古纳市| 秀山| 横山县| 吴桥县| 福泉市| 北票市| 富平县| 泰安市| 广东省| 鹤庆县| 裕民县| 高阳县| 如东县| 郯城县|