Qt 加载图片的几种方式 以及加载 loading

项目中经常使用加载图片:

常用有两种方式:

1.使用 QWidget 加载图片:

效果:

 

 样例源码:

    int pict_H = ui->widgetImage->height();
    int pict_W = ui->widgetImage->width();
    ui->widgetImage->setFixedSize( pict_W ,pict_H);
    QPixmap pixmap("C:/Users/A013237/Pictures/volume_2024623184252.png");
    QPalette palette;
    palette.setBrush(this->backgroundRole(), QBrush(pixmap.scaled(    // 缩放背景图.
                                                                      ui->widgetImage->size(),
                                                                      Qt::IgnoreAspectRatio,
                                                                      Qt::SmoothTransformation)));
   // this->setPalette(palette);
    ui->widgetImage->setAutoFillBackground(true);

    ui->widgetImage->setPalette(palette);

    ui->widgetImage->show();

2.使用 QLabel 加载图片:

    QLabel *label = new QLabel(&window);
    
    // 加载图片,替换为你的图片路径
    QPixmap pixmap("path_to_your_image.jpg");
 
    // 如果图片不在指定路径或尺寸问题,可以调整大小或使用一个默认的图片
    if (pixmap.isNull()) {
        label->setText("无法加载图片");
    } else {
        label->setPixmap(pixmap);
        label->setScaledContents(true); // 自动缩放以适应标签大小
    }
 
    // 在窗口中居中显示QLabel
    label->setGeometry(0, 0, window.width(), window.height());

缩放居中

QPixmap pix = QPixmap::fromImage(m_QImg);
ui.label->setAlignment(Qt::AlignCenter);
ui.label->setPixmap(pix.scaled(ui.label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); 

 图片适应控件(图片和控件大小一样)

ui.label->setScaledContents(true);

 控件适应图片

QPixmap pix = QPixmap::fromImage(m_QImg);
ui.label->setPixmap(pix); //图像显示在label上
ui.label->adjustSize(); //控件适应图像(注意必须放到上一句代码之后)

3. 加载 loading gif 图片

效果:

 源码样例:

#ifndef WAITING_H
#define WAITING_H

#include <QWidget>
#include <QLabel>
#include <QMovie>
 
namespace Ui {
class Waiting;
}

class Waiting : public QWidget
{
    Q_OBJECT

public:
    explicit Waiting(QWidget *parent = nullptr);
    ~Waiting();
 
private:
    QMovie *movie;
    QLabel *label;
    QLabel * tip_label;
    QFrame * background;
   
private:
    Ui::Waiting *ui;
};

#endif

cpp

#include "waiting.h"
#include "ui_waiting.h"
#include<QDebug>
Waiting::Waiting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Waiting)
{
    ui->setupUi(this);

    this->setFixedSize(400,400);
    background = new QFrame(this);
    background->setStyleSheet("background-color:#0000;border-radius:1px;");
    background->setGeometry(0, 50, 400,400);
    label = new QLabel(background);
    label->setGeometry(0, 0, 400,400);
    movie = new QMovie(":/Resources/loading-t.gif");
    movie->setScaledSize(QSize(400,400));
    label->setScaledContents(true);
    label->setMovie(movie);
    movie->start();


}

Waiting::~Waiting()
{
    delete ui;
}

使用:

#include "waiting.h"
void MainWindow::on_pushButton_loading_clicked()
{
    Waiting *w = new Waiting(this);
      w->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
      w->setWindowModality(Qt::ApplicationModal);
      w->move(880,450);
      w->show();

}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值