QPixmap加载BMP图片,内存溢出,怎么办?

你的OPPA 2015-01-19 01:50:03
初始化后两个label可以显示图片,当点击按钮切换两个label显示图片后,求报错:QImage: out of memory, returning null image。我的每张图片都是6512*11635的BMP图片,大小为72.2M,我觉得是加载的缓存区不够大导致的,需要每次显示需要清理缓存区,不知道对不对,求大神指点下
void MinImage::loadMinImage() //初始化
{
i = 1;
QDir dir("/image1","*.bmp");
if(!dir.exists())
return;
dir.setSorting(QDir::Time);//文件排列方式:时间

background1.load(QString("E:/image1/%1").arg(dir[dir.count()-i]));
background1 = background1.scaled(background1.width(),background1.height(),Qt::KeepAspectRatio);
label_image1->setPixmap(background1);
i++;
background2.load(QString("E:/image1/%1").arg(dir[dir.count()-i]));
background2 = background2.scaled(background2.width(),background2.height(),Qt::KeepAspectRatio);
label_image2->setPixmap(background2);
}

void MinImage::Right_Button_clicked()
{
QDir dir("/image1","*.bmp");
dir.setSorting(QDir::Time);
if(i==dir.count())
{
QMessageBox::warning(this,"warning","Has reached the final!");
}
else
{
i++;
background1.load(QString("E:/image1/%1").arg(dir[dir.count()-i+1]));
background1 = background1.scaled(background1.width(),background1.height(),Qt::KeepAspectRatio);
label_image1->setPixmap(background1);
background2.load(QString("E:/image1/%1").arg(dir[dir.count()-i]));
background2 = background2.scaled(background2.width(),background2.height(),Qt::KeepAspectRatio);
label_image2->setPixmap(background2);
}

}

void MinImage::Left_Button_clicked()
{
QDir dir("/image1","*.bmp");
dir.setSorting(QDir::Time);
if(i==2)
{
QMessageBox::warning(this,"warning","Has reached the final!");
}
else
{
i--;
background1.load(QString("E:/image1/%1").arg(dir[dir.count()-i+1]));
background1 = background1.scaled(background1.width(),background1.height(),Qt::KeepAspectRatio);
label_image1->setPixmap(background1);
background2.load(QString("E:/image1/%1").arg(dir[dir.count()-i]));
background2 = background2.scaled(background2.width(),background2.height(),Qt::KeepAspectRatio);
label_image2->setPixmap(background2);
qDebug()<<i;
}
}
...全文
1028 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaomick 2015-01-25
  • 打赏
  • 举报
回复
我也遇到这个问题了
dbzhang800 2015-01-19
  • 打赏
  • 举报
回复
引用 4 楼 u011330815 的回复:
[quote=引用 3 楼 dbzhang800 的回复:] 去看看QImageReader吧,或许对你有用 QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.
这样的话就要换QImage类型了啊,能不能用QPixmap[/quote] 你前面的用的QPixmap加载文件的代码,内部也是通过QImageReader 读入到QImage,而后转成 QPixmap 来实现的。 不然,你不觉得,你没有用QImage,但是报错的却是QImage,很奇怪么?
你的OPPA 2015-01-19
  • 打赏
  • 举报
回复
引用 3 楼 dbzhang800 的回复:
去看看QImageReader吧,或许对你有用 QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.
这样的话就要换QImage类型了啊,能不能用QPixmap
dbzhang800 2015-01-19
  • 打赏
  • 举报
回复
去看看QImageReader吧,或许对你有用 QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.
你的OPPA 2015-01-19
  • 打赏
  • 举报
回复
怎么还没有人啊!哪个大神帮忙指点一下啊!急······
你的OPPA 2015-01-19
  • 打赏
  • 举报
回复
经过调试,问题就是在点击按钮后出发的函数里面的load加载图片。我用QPixmapCache::clear()也没有用,该肿么办?求大神指点

16,212

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧