QT如何通過鼠標(biāo)事件實現(xiàn)圖片的拖動和縮放
通過鼠標(biāo)拖動來移動圖片,并使用鼠標(biāo)滾輪來縮放圖片。
1、實現(xiàn)步驟:
1、移動圖片:
使用QPoint記錄圖片的偏移量,當(dāng)鼠標(biāo)拖動時更新這個偏移量,在paintEvent()中根據(jù)偏移量繪制圖片。
2、縮放圖片:
使用滾輪事件調(diào)整圖片的縮放倍數(shù),在paintEvent()中重新繪制圖片時應(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)移動事件
void mouseMoveEvent(QMouseEvent *event) override;
// 鼠標(biāo)釋放事件
void mouseReleaseEvent(QMouseEvent *event) override;
// 滾輪事件,用于縮放圖片
void wheelEvent(QWheelEvent *event) override;
private:
QPixmap pixmap; // 圖片
QPoint lastMousePosition; // 上一次鼠標(biāo)點擊的位置
QPoint offset; // 圖片的偏移量
bool isDragging; // 標(biāo)記是否正在拖動圖片
double scaleFactor; // 縮放倍數(shù)
};
#endif // IMAGEWIDGET_H2、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)下不拖動
offset = QPoint(0, 0); // 圖片的初始偏移為(0, 0)
}
// 重寫paintEvent,用于繪制圖片
void ImageWidget::paintEvent(QPaintEvent *event)
{
// 創(chuàng)建一個QPainter,用于繪制圖片
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform); // 開啟平滑縮放
// 計算縮放后的圖片尺寸
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; // 開始拖動
lastMousePosition = event->pos(); // 記錄鼠標(biāo)點擊位置
}
}
// 鼠標(biāo)移動事件
void ImageWidget::mouseMoveEvent(QMouseEvent *event)
{
if (isDragging) {
// 計算鼠標(biāo)的移動距離
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é)束拖動
}
}
// 滾輪事件,用于縮放圖片
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()方法用于自定義控件的繪制。在這個方法中,我們使用QPainter對象來繪制圖片。
pixmap.scaled():使用pixmap.scaled()方法來縮放圖片,根據(jù)scaleFactor縮放圖片并保持其寬高比。
painter.drawPixmap(offset, ...):在指定的偏移量位置繪制圖片,offset表示圖片在控件中的偏移。
2. 圖片移動:
mousePressEvent():當(dāng)鼠標(biāo)左鍵按下時,記錄鼠標(biāo)點擊位置,并開始拖動圖片(isDragging = true)。
mouseMoveEvent():如果正在拖動圖片(isDragging為true),則計算鼠標(biāo)的移動距離,將圖片的offset偏移量更新為原來的偏移量加上鼠標(biāo)移動的距離。
mouseReleaseEvent():當(dāng)鼠標(biāo)左鍵釋放時,結(jié)束拖動(isDragging = false)。
3. 圖片縮放:
wheelEvent():處理鼠標(biāo)滾輪事件。滾輪向上時,放大圖片;滾輪向下時,縮小圖片。
scaleFactor *= 1.1:每次滾動時,縮放倍數(shù)按10%變化。
qBound(0.1, scaleFactor, 10.0):限制縮放比例在0.1到10倍之間,防止圖片縮得太小或放得太大。
4. 平滑縮放:
Qt::SmoothTransformation:使用平滑縮放算法,防止圖片縮放后出現(xiàn)鋸齒。
5. 偏移量的作用:
offset用于記錄圖片相對于窗口的偏移量,當(dāng)鼠標(biāo)拖動時,更新這個偏移量。
在paintEvent()中,每次繪制時都使用這個偏移量來控制圖片的位置。
運行效果:
拖動圖片:按住鼠標(biāo)左鍵并移動,可以看到圖片在窗口內(nèi)部移動。
縮放圖片:滾動鼠標(biāo)滾輪,圖片會放大或縮小,同時保持寬高比。
總結(jié)
到此這篇關(guān)于QT如何通過鼠標(biāo)事件實現(xiàn)圖片的拖動和縮放的文章就介紹到這了,更多相關(guān)QT鼠標(biāo)事件實現(xiàn)圖片拖動縮放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中默認(rèn)無參構(gòu)造函數(shù)的工作機(jī)制淺析
構(gòu)造函數(shù)主要作用在于創(chuàng)建對象時為對象的成員屬性賦值,構(gòu)造函數(shù)由編譯器自動調(diào)用,無須手動調(diào)用;析構(gòu)函數(shù)主要作用在于對象銷毀前系統(tǒng)自動調(diào)用,執(zhí)行一些清理工作2023-02-02
詳解如何配置CLion作為Qt5開發(fā)環(huán)境的方法
這篇文章主要介紹了詳解如何配置CLion作為Qt5開發(fā)環(huán)境的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
C++ vector擴(kuò)容解析noexcept應(yīng)用場景
這篇文章主要介紹了C++ vector擴(kuò)容解析noexcept應(yīng)用場景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
C++標(biāo)準(zhǔn)模板庫string類的介紹與使用講解
今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫string類的介紹與使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

