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

C++基于QWidget和QLabel實(shí)現(xiàn)圖片縮放,拉伸與拖拽

 更新時間:2024年02月28日 10:26:30   作者:隨性隨筆  
這篇文章主要為大家詳細(xì)介紹了C++如何基于QWidget和QLabel實(shí)現(xiàn)圖片縮放、拉伸與拖拽等功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

最近做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;//在當(dāng)前的比例基礎(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ù),確保默認(rèn)行為也得到執(zhí)行
    QWidget::resizeEvent(event);
}

測試入口代碼

QPictureShot *pic = new QPictureShot(this);
    pic->inputPicFile(":/bg.png");
    pic->show();

到此這篇關(guān)于C++基于QWidget和QLabel實(shí)現(xiàn)圖片縮放,拉伸與拖拽的文章就介紹到這了,更多相關(guān)C++圖片縮放拉伸與拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • QT5實(shí)現(xiàn)UDP通信的示例代碼

    QT5實(shí)現(xiàn)UDP通信的示例代碼

    本文主要介紹了QT5實(shí)現(xiàn)UDP通信的示例代碼,主要使用QUdpSocket類用于實(shí)現(xiàn)UDP通信,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • C語言函數(shù)指針詳解

    C語言函數(shù)指針詳解

    大家好,本篇文章主要講的是C語言函數(shù)指針詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • 深入了解c++11 移動語義與右值引用

    深入了解c++11 移動語義與右值引用

    這篇文章主要介紹了c++ 移動語義與右值引用的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下
    2020-08-08
  • c++中std::placeholders的使用方法

    c++中std::placeholders的使用方法

    std::placeholders?是 C++ 標(biāo)準(zhǔn)庫中的一個工具,用于在函數(shù)對象綁定時創(chuàng)建占位符,本文就來詳細(xì)的介紹一下,具有一定的參考價值,感興趣的可以了解一下
    2025-02-02
  • C++11實(shí)現(xiàn)簡易定時器的示例代碼

    C++11實(shí)現(xiàn)簡易定時器的示例代碼

    這篇文章主要介紹了C++11實(shí)現(xiàn)簡易定時器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 使用VC++實(shí)現(xiàn)打印乘法口訣表

    使用VC++實(shí)現(xiàn)打印乘法口訣表

    本文給大家分享的是一個超級簡單的小例子,使用vc++打印乘法口訣表,給需要的小伙伴參考下吧。
    2015-03-03
  • C++之list的使用與模擬實(shí)現(xiàn)過程

    C++之list的使用與模擬實(shí)現(xiàn)過程

    本文簡述C++?list容器,其基于雙向循環(huán)鏈表,支持高效插入刪除,迭代器失效僅影響被刪除節(jié)點(diǎn),模擬實(shí)現(xiàn)時通過封裝節(jié)點(diǎn)和迭代器,對比list與vector的特性差異,如隨機(jī)訪問、空間利用率及適用場景
    2025-10-10
  • 深入剖析Android中init進(jìn)程實(shí)現(xiàn)的C語言源碼

    深入剖析Android中init進(jìn)程實(shí)現(xiàn)的C語言源碼

    這篇文章主要介紹了Android中init進(jìn)程實(shí)現(xiàn)的C語言源碼,init屬性服務(wù)在安卓中屬于系統(tǒng)的底層Linux服務(wù),需要的朋友可以參考下
    2015-07-07
  • qt首次連接MYSQL驅(qū)動的各種問題圖文詳解

    qt首次連接MYSQL驅(qū)動的各種問題圖文詳解

    通常來說,我們對數(shù)據(jù)庫的操作更多地在于對數(shù)據(jù)庫表的操作,下面這篇文章主要給大家介紹了關(guān)于qt首次連接MYSQL驅(qū)動的各種問題,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • VC++ loadlibrary()加載三方dll失敗, 返回錯誤碼:126的解決方法

    VC++ loadlibrary()加載三方dll失敗, 返回錯誤碼:126的解決方法

    今天在編寫VC++ loadlibrary()加載三方dll是總是失敗,并且返回錯誤碼:126,這里就為大家分享一下具體的解決方法
    2021-03-03

最新評論

平武县| 兴城市| 西贡区| 广元市| 郁南县| 石柱| 太湖县| 西乡县| 康定县| 张家口市| 泸西县| 玉树县| 大名县| 利辛县| 洛浦县| 广东省| 南昌县| 桓台县| 天长市| 余庆县| 孟津县| 大丰市| 商都县| 淅川县| 闻喜县| 昭通市| 吉安县| 密云县| 瑞昌市| 三台县| 柞水县| 吉木萨尔县| 缙云县| 遵义县| 安达市| 岑溪市| 北宁市| 冷水江市| 太白县| 石泉县| 嘉峪关市|