1

我的 Qt GUI 上有一系列QLabel对象,我用 HBITMAP 对象填充它们。这些 HBITMAP 是内存中的缓冲区,它们不存在于磁盘上。

现在我使用QPixmaps fromWinHBITMAP to create aQPixmap which I can then pass to theQLabel ssetPixmap` 函数。

现在,问题是,当我用另一个覆盖它时,QLabel 中的当前图像会发生什么,它会留在内存中吗?它会被删除吗?

我怀疑它没有被正确删除,因为我的程序在运行大约一个小时后增长到巨大的比例。(1.7GB) 内存。

进行转换的代码是:

//buffer is a map of QLabels which are filled with images.
void LoadPixmapFromBitmap(HBITMAP hBitmap, std::map<int, QLabel*>& buffer, int pixmapindex)
{
    QPixmap pix;
    pix = QPixmap::fromWinHBITMAP(hBitmap);

    QPixmap temp(pix);      
    QSize sz(164, 121);
    QPixmap resized(temp.scaled(sz));

    QMatrix rotation;
    rotation.rotate(90);
    QPixmap rotated = resized.transformed(rotation);

//an attempt to delete the previous image properly and put in a new one.  This doesn't seem to work.
    if (buffer[pixmapindex]->pixmap() != NULL)
    {
        HBITMAP hbtmp = buffer[pixmapindex]->pixmap()->toWinHBITMAP();
        buffer[pixmapindex]->clear();

        HDC dc = GetDC(this->winId());
        //HBITMAP p_old = SelectObject(dc, hbtmp);

        BOOL deleted = DeleteObject(hbtmp);
        if (!deleted)
            PrintMsg("temp not deleted");
    }

//////////////////////////////////end of attempt
    buffer[pixmapindex]->setPixmap(rotated);

    BOOL success = DeleteObject(hBitmap);
    if (!success)
        PrintMsg("hBitmap was not deleted");
}
4

1 回答 1

2

QPixmap::fromWinHBITMAP制作给定位图的副本,而不是别名。

您应该在转换为 之后删除原始位图QPixmap,因为调用oWinHBITMAP会复制(再次)位图,存储在给定的像素图中,但不会为您提供原始 Windows 位图的句柄。

于 2012-02-24T17:06:43.710 回答