user3020345
user3020345

Reputation: 19

loading an image on button click in QT gui

I am a beginner in QT gui c++ programming. i am trying to load a picture on button click using a pushbutton and label. inside my mainwindow.cpp , i only added the following.

      void MainWindow::on_pushButton_clicked()
    {
     QPixmap pix("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");
      ui->img_label->setPixmap(pix);
    }

. thats all wat i added in the code. program compiles fine but when it runs, it just shows a portion of image instead of showing the full image. can anyone help. Without even adding to the resources,it showed atleast a part of the image.

Upvotes: 0

Views: 1933

Answers (2)

gooddocteur
gooddocteur

Reputation: 109

You can scale your image to specific size something like that:

void MainWindow::on_pushButton_clicked() {
  QPixmap pix("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");
  ui->img_label->setPixmap(pix.scaled(my_width, my_height, 
                           Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

If you don't want scale, tell us what layout manager do you use?

Upvotes: 0

sarahara
sarahara

Reputation: 65

you can try using PNG - as depending on your Qt version some plugins are needed for JPG. You should also make the pixmap a member of your class.

Upvotes: 1

Related Questions