Scaling QPixmap to maximized screen size

  • I am having a problem of scaling a QPixmap to the maximized QMainWindow resolution. Hoping someone can steer me in the right direction. Iv'e tried using size() and sizeHint to figure out the maxium size the QPixmap should be.

    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        player = new QMediaPlayer;
        vw = new QVideoWidget;
        gv = new QGraphicsView;
        scene = new QGraphicsScene;
        pixmap = new QGraphicsPixmapItem;
        //playVideo(QString("C:/Users/bseishen/Desktop/Wildlife/Juvenile Fish/Salmon_Watch.mp4"));
    void MainWindow::showImage(QString file){
        QPixmap img = QPixmap(file);
        QSize widgetSize = centralWidget()->sizeHint();
        img = img.scaled(widgetSize,Qt::KeepAspectRatioByExpanding);
        qDebug() << widgetSize.height() << " " << widgetSize.width();
        qDebug() << this->height() << " " << this->width();
        qDebug() << centralWidget()->height() << " " << centralWidget()->width();

  • Solved it with a qApp->processEvents(); wasnt allowing the GUI to catch up with the window resize.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It would be cleaner to trigger that using a QTimer::singleShot with a 0 timeout. That way you would allow your application loop to start.

    By the way, why not do all the setup in your constructor and only set the new pixmap in showImage ?

Log in to reply

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