Important: Please read the Qt Code of Conduct -

Draw on the center of widget

  • I'm trying to draw an image on the center of a widget using the paintEvent and I'm doing like this:

    QPainter painter;
    painter.drawPixmap(QPoint(width() / 2 - mLogotype->width() / 2, height() / 2 - mLogotype->height() / 2), mLogotype);

    Is there a more convenient way for doing that?

    The mLogotype is a QPixmap.

  • Moderators

    You already have it in one line so there's little to be improved, but you can also do:

    painter.drawPixmap((rect().bottomRight() - mLogotype.rect().bottomRight()) / 2, mLogotype);

    Btw. In your example there's QPointer. I think you meant QPoint.

  • @Chris-Kawa Yes, it's point and thank you for the improvement. :D

Log in to reply