Domanda

Ho una serie di QLabel Oggetti sulla mia GUI QT e li riempii con oggetti HBITMAP. Questi hbitmap sono buffer in memoria, non esistono sul disco.

Ora uso il QPixmapS dawinhbitmapto create aQpixmapwhich I can then pass to theQlabelsfunzione setpixmap`.

Ora, la domanda è: cosa succede all'immagine corrente nel Qlabel, quando la sovrascrivo con un altro, rimane in memoria? Viene eliminato?

Sospetto che non venga eliminato correttamente, poiché il mio programma cresce in enormi proporzioni dopo aver funzionato per circa un'ora. (1,7 GB) in memoria.

Il codice che fa la conversione è:

//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");
}
È stato utile?

Soluzione

QPixmap::fromWinHBITMAP Crea una copia di Bitmap data, non alias One.

Dovresti eliminare la bitmap originale subito dopo la conversione QPixmap, perché chiama a oWinHBITMAP fa una copia (ancora) di bitmap, leggendano in una determinata pixmap, ma non ti dà la gestione della bitmap di Windows originale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top