Search code examples
c++qwidgetqpixmap

QPixmap, how to make sure it is 'always on top'


For Widgets I can call 'raise' which keeps the widget on top of anything else, but this doesn't seem to work for any QPixmap's that are rendered.

How can I ensure that a QPixmap remains on top of anything else?

In my paintEvent function:

    QPainter objPainter(this);

    if ( strImage.isEmpty() != true ) {
        qint16 int16NudgeImageX = mpobjNode->int16GetAttr(clsXML::mscszAttrNudgeImageX)
              ,int16NudgeImageY = mpobjNode->int16GetAttr(clsXML::mscszAttrNudeImageY);
        QPixmap pmImage(":/" + strImage);
        QSize szImage = pmImage.size();
        QPoint ptImage(rctGeom.center().x() - (szImage.width() / 2) + int16NudgeImageX
                      ,rctGeom.center().y() - (szImage.height() / 2) + int16NudgeImageY);
        QRect rctImage(ptImage, szImage);
        objPainter.drawPixmap(rctImage, pmImage);
    }

Solution

  • A QPixmap is not a widget (no parent, nor layout).

    The QPixmap class is an off-screen image representation that can be used as a paint device.

    Use a QLabel for use with a parent (and layout)

    QLabel* label = new QLabel(parent);
    label->setPixmap(pixmap);