【QT】对话框打开图像并用QPixmap显示

时间:2023-03-09 18:38:24
【QT】对话框打开图像并用QPixmap显示

绘图设备是指继承QPaintDevice的子类,可以使用QPainter直接在其上面绘制图形,Qt一共提供了四个这样继承QPaintDevice的绘图设备类。

分别是QPixmap、QBitmap、QImage和 QPicture。

#include <QPixmap>
   //打开文件对话框
QString lastPath="D:/Englishpath/QTprojects/DATA/videoData";
fileName = QFileDialog::getOpenFileName(this, tr("打开文件"), lastPath);
if(fileName.isEmpty())
{
return;
}
else
{
QImage* img=new QImage; if(! ( img->load(fileName) ) ) //加载图像
{
QMessageBox::information(this,
tr("打开图像失败"),
tr("打开图像失败!"));
delete img;
return;
}
ui->d_label->setPixmap(QPixmap::fromImage(*img));
}

或者直接最简单,

    //打开文件对话框
QString lastPath="D:/Englishpath/QTprojects/DATA/videoData";
fileName = QFileDialog::getOpenFileName(this, tr("打开文件"), lastPath); QPixmap p;
p.load(fileName);
ui->d_label->setPixmap(p);

区别是:

QPixmap依赖于硬件,QImage不依赖于硬件。

QPixmap主要是用于绘图,针对屏幕显示而最佳化设计,QImage主要是为图像I/O、图片访问和像素修改而设计的。

当图片小的情况下,直接用QPixmap进行加载,画图时无所谓,当图片大的时候如果直接用QPixmap进行加载,会占很大的内存,一般一张几十K的图片,用QPixmap加载进来会放大很多倍。

所以一般图片大的情况下,用QImage进行加载,然后转乘QPixmap用户绘制。QPixmap绘制效果是最好的(第一种代码的方式)。

【转载自】

Qt中图像的显示与基本操作 - romi - 博客园 https://www.cnblogs.com/Romi/archive/2012/03/14/2396533.html

QPixmap用法总结 - doupi2008的专栏 - CSDN博客 https://blog.csdn.net/doupi2008/article/details/44960003

显示图像的铺满设置。

qt 中 QPixmap 类的使用及用 QLabel 显示图像-keanight的博客 - CSDN博客 https://blog.csdn.net/keanight/article/details/79150637