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) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        this->showMaximized();
    
        player = new QMediaPlayer;
        vw = new QVideoWidget;
        player->setVideoOutput(vw);
    
        gv = new QGraphicsView;
        scene = new QGraphicsScene;
        pixmap = new QGraphicsPixmapItem;
    
    
    
        //playVideo(QString("C:/Users/bseishen/Desktop/Wildlife/Juvenile Fish/Salmon_Watch.mp4"));
        showImage(QString("C:/Users/bseishen/Desktop/media/1_fishLadder/Slide1.JPG"));
    
    
    }
    
    void MainWindow::showImage(QString file){
    
        this->setCentralWidget(gv);
    
        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();
    
        pixmap->setPixmap(img);
        gv->setScene(scene);
        scene->addItem(pixmap);
    
    }
    


  • 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.