Scale QPixmap has no effect

  • Hi guys,

    I am coding an app for my android tablet. I am trying to set a png image as background to my mainwindow. However, the scaled() fct doesn't resize the QPixmap to the right size.

    Here is my code :

    QPixmap bkgnd = QPixmap(":/images/0.png");
    QPixmap scaledPixmap = bkgnd.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    QPalette palette;
    QBrush brush = QBrush(scaledPixmap);
    palette.setBrush(QPalette::Background, brush);

    Thank you for your help !

  • Qt Champions 2017

    Could you illustrate the issue with a screenshot, as we can have no idea what the correct size is?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you by any chance doing that in the constructor ? If so that's normal. When the constructor is called the widget has no idea about the size it's going to have. You should rather do that in a showEvent reimplementation or maybe resizeEvent.

  • Wow you are right ! Now it works as expected !

    Thank you so much, I've been looking for a solution for like a week !

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.