PDA

View Full Version : Display pictures using label?



Kloster
1st September 2016, 07:43
Hi there,

I tried to make use of the GUi editor in QT creator and apply images to my project using a label as displayed on youtube. but they haven't seemed to work. Can someone teach me how to add multiple pictures to my project? I also tried to include Qpixmap and link to it that way but they still didn't show up when I ran my project.

I have attached a picture of what I would like it to look like and my project code is currently as follows


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPixmap"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
ui->label_5->setText("Pump On");
}

void MainWindow::on_pushButton_2_clicked()
{
ui->label_6->setText("Pump On");
}

void MainWindow::on_pushButton_3_clicked()
{
ui->label_7->setText("Pump On");
}

anda_skoa
1st September 2016, 09:35
In Designer you can specify either a file name on disk or from the resource system.

In code that is basically the same, you can pass a filename from disk or the resource system to QPixmap and put that on the label.

Cheers,
_

Kloster
1st September 2016, 09:39
Yep, understand that, and have done that both ways passing the filename in code using Qpixmap and in designer specifying from file in there, but when i run the project the pictures don't load.

anda_skoa
1st September 2016, 12:18
Have you checked the paths?

If they are relative paths the current directory could be different.

Cheers,
_

d_stranz
1st September 2016, 18:02
It is generally better to learn how to embed small images like icons into the resource (qrc) file (http://doc.qt.io/qt-5/resources.html) so they get compiled into your program and don't depend on external disk files. As the docs say, when compiling, the image files usually need to be in the same directory as the qrc file, but once compiled and linked into a binary (exe or lib/dll) they get loaded at runtime from the binary.

Nejir
2nd September 2016, 08:55
Try something like this:


QPixmap pictureToDisplay = QPixmap("This/is/your/path.jpg");
//scale it if you want to
QPixmap scaledImage = pictureToDisplay.scaled(ui->myLabel->size(),Qt::KeepAspectRatio,Qt::SmoothTransformatio n);
ui->myLabel->setPixmap(scaledImage);

Kloster
2nd September 2016, 10:40
Thanks all for your help,

It is strange no matter if I use code or try add via the designer platform the picture still doesn't want to load.

I will just continue with the rest of the project and come back to this later, I will post the full code here when ready for help on the picture additions.. Its not project critical that the pictures show.

anda_skoa
2nd September 2016, 12:02
Have you verified, e.g. using QFileInfo, that the path is correct?

Does QimageReader::supportedImageFormats() list "jpg" (or whatever format your images are in)?

Cheers,
_