欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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++ 遞歸鎖的使用示例代碼

    c++ 遞歸鎖的使用示例代碼

    這篇文章主要介紹了c++ 遞歸鎖的使用示例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • OpenSSL動態(tài)鏈接庫源碼安裝教程

    OpenSSL動態(tài)鏈接庫源碼安裝教程

    Openssl 是一個開放源代碼的SSL協(xié)議的產(chǎn)品實現(xiàn),它采用C語言作為開發(fā)語言,具備了跨系統(tǒng)的性能。這篇文章主要介紹了OpenSSL動態(tài)鏈接庫源碼安裝,需要的朋友可以參考下
    2021-11-11
  • C程序讀取鍵盤碼的方法

    C程序讀取鍵盤碼的方法

    這篇文章主要介紹了C程序讀取鍵盤碼的方法,運行時可通過鍵盤按鍵獲取其對應(yīng)的鍵盤碼,文章最后附帶了鍵盤碼與按鍵的對照表,需要的朋友可以參考下
    2014-09-09
  • C語言實現(xiàn)簡單三子棋游戲

    C語言實現(xiàn)簡單三子棋游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單三子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng)

    C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Qt視頻播放器的實現(xiàn)示例

    Qt視頻播放器的實現(xiàn)示例

    本文主要介紹了Qt視頻播放器的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-08-08
  • C++引用的詳細解釋

    C++引用的詳細解釋

    以下是對C++中引用的使用進行了詳細的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助,希望能夠給你帶來幫助
    2021-11-11
  • C++實現(xiàn)LeetCode(110.平衡二叉樹)

    C++實現(xiàn)LeetCode(110.平衡二叉樹)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(110.平衡二叉樹),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • c++中比較好用的“黑科技”

    c++中比較好用的“黑科技”

    這篇文章主要介紹了c++中比較好用的“黑科技”,一些常用小編沒有給大家羅列出,主要給大家介紹了sort函數(shù),需要的朋友可以參考下
    2020-02-02
  • VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動態(tài)庫dll的問題 附源碼

    VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動態(tài)庫dll的問題 附源碼

    這篇文章主要介紹了VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動態(tài)庫(dll)(文末附源碼),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03

最新評論