当前位置: 代码迷 >> QT开发 >> QPix地图加载BMP图片,内存溢出,怎么处理
  详细解决方案

QPix地图加载BMP图片,内存溢出,怎么处理

热度:304   发布时间:2016-04-25 03:02:25.0
QPixmap加载BMP图片,内存溢出,怎么办?
初始化后两个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;
    }
}
------解决思路----------------------
引用:
Quote: 引用:

去看看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


你前面的用的QPixmap加载文件的代码,内部也是通过QImageReader 读入到QImage,而后转成 QPixmap 来实现的。

不然,你不觉得,你没有用QImage,但是报错的却是QImage,很奇怪么?
  相关解决方案