How to display the file "* .png" in the user interface in QT?

I am new to Qt Framework ...

I want to display pp pp in my Form1.ui, so I dragged the Graphics view from the widget window, then I placed test.png in the same directory (inside the project folder)

and I did it in code

//Form1.cpp
#include "form1.h"
#include "ui_form1.h"

Form1::Form1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form1)
{
    ui->setupUi(this);
    ui->Pic1->setStyleSheet("background-image: url(test.png)");

}

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



//Form1.h
#ifndef FORM1_H
#define FORM1_H

#include <QWidget>

namespace Ui {
    class Form1;
}

class Form1 : public QWidget
{
    Q_OBJECT

public:
    explicit Form1(QWidget *parent = 0);
    ~Form1();

private:
    Ui::Form1 *ui;
};

#endif // FORM1_H

It compiled perfectly, but the rice didn’t appear. What am I wrong?

this is my qrc:

+5
source share
3 answers

The widget you should use to display images is QLabel. You can do this directly from QtCreator by setting its property pixmap.

, , . Qt, : > Qt > Qt.

EDIT :

//names starting with : means that they are on a resource file, 
//otherwise in the filesystem
QPixmap * mypix = new QPixmap(":/karim/test.png"); 
ui->your_label->setPixmap(mypix);
delete mypix;
+11

If you have png in your resources, maybe change your background-image: tag:

ui->Pic1->setStyleSheet("background-image: url(:/test.png)");
0
source

All Articles