开发者

How to Display a "*.png" file on a UI in QT framework?

I am new to Qt Framework...

I want to display a .png pic in my Form1.ui , so I dragged and dropped a Graphic view from the widget box then I placed test.png in the same directory (inside the project folder)

and i did this in the 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();

pri开发者_运维百科vate:
    Ui::Form1 *ui;
};

#endif // FORM1_H

It compiled perfectly but the pic didn't appear , What did I Wrong ?

this is my qrc :


The Widget you should use to show pictures is a QLabel. You can do it directly from QtCreator, by setting its pixmap property.

As others said, you should first create a resource file and then add the image to that resource file. To create a Qt Resource File, go to the menus: File > Qt > Qt Resource File.

EDIT To do it programatically:

//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;


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

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


You need to add the image to a resource file: http://doc.qt.io/qt-5/resources.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜