1

Qt GUIに一連のQLabelオブジェクトがあり、それらにHBITMAPオブジェクトを入力します。これらのHBITMAPはメモリ内のバッファであり、ディスク上には存在しません。

ここで、QPixmaps fromWinHBITMAP to create aQPixmapQLabelsetPixmap`which I can then pass to the関数を使用しsます。

さて、問題は、QLabelの現在の画像を別の画像で上書きするとどうなるかということです。画像はメモリに残りますか?削除されますか?

私のプログラムは約1時間実行した後、非常に大きな割合で成長するため、適切に削除されないのではないかと思います。(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のコピー(再度)が作成されますが、元のウィンドウのビットマップへのハンドルは提供されないためです。

于 2012-02-24T17:06:43.710 に答える