C++基于QWidget和QLabel實現(xiàn)圖片縮放,拉伸與拖拽
更新時間:2024年02月28日 10:26:30 作者:隨性隨筆
這篇文章主要為大家詳細介紹了C++如何基于QWidget和QLabel實現(xiàn)圖片縮放、拉伸與拖拽等功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
最近做ffmpeg視頻拉流解碼,涉及到截圖功能的圖片顯示操作,特此做記錄。
頭文件
#ifndef QPICTURESHOT_H #define QPICTURESHOT_H #include <QWidget> #include <QLabel> class QPictureShot : public QWidget { Q_OBJECT public: explicit QPictureShot(QWidget *parent = nullptr); ~QPictureShot(); //接收一張圖片文件路徑 void inputPicFile(QString sFile); //設(shè)置定時消失 void setAutoDismiss(int nShowSeconds); protected: private: //初始化窗體部件 void InitLayout(); void wheelEvent(QWheelEvent * event) override; void resizeEvent(QResizeEvent* event) override ; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; QLabel *m_pPicLabel; //圖片 QImage m_image; QRect m_initRect; double opacityValue;//窗體初始化透明度 float ratio = 1.0f; bool m_bLeftPress;//左鍵按下 float m_difX; float m_difY; }; #endif // QPICTURESHOT_H
cpp文件:
#include "qpictureshot.h" #include <QApplication> #include <QDesktopWidget> #include <QPainter> #include <QTimer> #include <QMouseEvent> #include <QDebug> #ifdef WIN32 #pragma execution_character_set("utf-8") #endif QPictureShot::QPictureShot(QWidget *parent): QWidget(parent), m_pPicLabel(new QLabel(this)), opacityValue(1.0) { setWindowFlags(Qt::Window |Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint); setWindowTitle("圖片"); this->setAttribute(Qt::WA_TranslucentBackground); InitLayout(); } QPictureShot::~QPictureShot() { } //接收一張圖片文件路徑 void QPictureShot::inputPicFile(QString sFile) { int nPos = sFile.lastIndexOf("/"); QString sText = sFile.right(sFile.length() - nPos - 1); setWindowTitle(sText); m_image = QImage(sFile); if (m_image.isNull()) { m_pPicLabel->setStyleSheet("font-size:18pt;color:white;"); m_pPicLabel->setAlignment(Qt::AlignCenter); m_pPicLabel->setText("文件不存在"); } else { QPixmap pix = QPixmap::fromImage(m_image); pix = pix.scaled(m_pPicLabel->width(), m_pPicLabel->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); m_pPicLabel->setScaledContents(true); m_pPicLabel->setPixmap(pix); } } //設(shè)置定時消失 void QPictureShot::setAutoDismiss(int nShowSeconds) { QTimer *mtimer = new QTimer(this);//隱藏的定時器 mtimer->setTimerType(Qt::PreciseTimer); connect(mtimer,&QTimer::timeout,this,[=](){ if(opacityValue<=0){ this->close(); } opacityValue = opacityValue-0.1; this->setWindowOpacity(opacityValue); //設(shè)置窗口透明度 }); QTimer *mShowtimer = new QTimer(this);//顯示時間的定時器 mShowtimer->setTimerType(Qt::PreciseTimer);// 修改定時器對象的精度 connect(mShowtimer,&QTimer::timeout,this,[=](){ mtimer->start(100);//執(zhí)行延時自動關(guān)閉 }); mShowtimer->start(nShowSeconds); } //初始化窗體部件 void QPictureShot::InitLayout() { //設(shè)置部件大小 QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以 this->resize(desktop->width() * 0.8, desktop->height() * 0.8); m_pPicLabel->setGeometry(0, 0, desktop->width() * 0.8, desktop->height() * 0.8); m_pPicLabel->setCursor(QCursor(Qt::PointingHandCursor)); m_initRect = this->rect(); } void QPictureShot::wheelEvent(QWheelEvent * event) { if (m_image.isNull()) return; QPoint numDegrees = event->angleDelta() / 120; float ratio_temp = (1 + numDegrees.y() / 20.0); ratio *= ratio_temp;//在當前的比例基礎(chǔ)上乘以 if (ratio > 3 || ratio < 0.05) return; int w = ratio_temp*m_pPicLabel->width(); //顯示寬度 int h = ratio_temp*m_pPicLabel->height(); //顯示高度 QPoint mousePos = event->pos(); QPoint picPos = m_pPicLabel->pos(); int pageWidth = mousePos.x() - picPos.x(); int pageHeight = mousePos.y() - picPos.y(); QPixmap pix = QPixmap::fromImage(m_image); pix = pix.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); //圖像縮放到指定高度和寬度,保持長寬比例 m_pPicLabel->setScaledContents(true); m_pPicLabel->setPixmap(pix); int nMoveX = pageWidth * (numDegrees.y() / 20.0); int nMoveY = pageHeight * (numDegrees.y() / 20.0); m_pPicLabel->setGeometry(m_pPicLabel->pos().x() - nMoveX, m_pPicLabel->pos().y() - nMoveY, w, h); } void QPictureShot::mousePressEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton) { m_bLeftPress = true; m_difX = event->pos().x() - m_pPicLabel->x(); m_difY = event->pos().y() - m_pPicLabel->y(); } } void QPictureShot::mouseMoveEvent(QMouseEvent *event) { if (m_bLeftPress) { int x = event->pos().x() - m_difX; int y = event->pos().y() - m_difY; m_pPicLabel->move(x, y); } } void QPictureShot::mouseReleaseEvent(QMouseEvent *event) { Q_UNUSED(event); m_bLeftPress = false; } void QPictureShot::resizeEvent(QResizeEvent* event) { // 在此處添加對窗口拉伸事件的處理邏輯 // 輸出新的大小信息 qDebug("New size: %dx%d", width(), height()); float x_ratio = width() * 1.0 / m_initRect.width(); float y_ratio = height() * 1.0 / m_initRect.height(); m_pPicLabel->setGeometry(m_pPicLabel->x() * x_ratio, m_pPicLabel->y() * y_ratio, m_pPicLabel->width() * x_ratio, m_pPicLabel->height() * y_ratio); if (!m_image.isNull()) { QPixmap pix = QPixmap::fromImage(m_image); pix = pix.scaled(this->width(), this->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); m_pPicLabel->setScaledContents(true); m_pPicLabel->setPixmap(pix); } m_initRect = this->rect(); // 調(diào)用基類的resizeEvent()函數(shù),確保默認行為也得到執(zhí)行 QWidget::resizeEvent(event); }
測試入口代碼
QPictureShot *pic = new QPictureShot(this); pic->inputPicFile(":/bg.png"); pic->show();
到此這篇關(guān)于C++基于QWidget和QLabel實現(xiàn)圖片縮放,拉伸與拖拽的文章就介紹到這了,更多相關(guān)C++圖片縮放拉伸與拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12C++實現(xiàn)LeetCode(110.平衡二叉樹)
這篇文章主要介紹了C++實現(xiàn)LeetCode(110.平衡二叉樹),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動態(tài)庫dll的問題 附源碼
這篇文章主要介紹了VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動態(tài)庫(dll)(文末附源碼),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03