Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to attach image to gui created by using Qt creator

Tags:

c++

qt

How to put an image on the GUI created by Qt? The south west part of the screen is empty so I want a picture to be put there on a click of a button but I am unable to use QPixmap and setPixmap. Please help me with this !

like image 814
Medha Anand Avatar asked Jul 03 '13 06:07

Medha Anand


People also ask

How do I insert a picture into Qt?

Using Qt Designer Drag this onto the QMainWindow to add it. Next, with the Label selected, look in the right hand QLabel properties panel for the pixmap property (scroll down to the blue region). From the property editor dropdown select "Choose File…" and select an image file to insert.

How do I add an image to Qwidget?

What you want is QPixmap . Give a filename to the QPixmap constructor, and set this pixmap to the label using QLabel. setPixmap() . Show activity on this post.

How do I add an image to a QLabel in Qt?

ui->label->setStyelSheet("image:url(:/1. png); border-image:url(:/2. png);");

How do I create a PNG file in Qt?

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.


2 Answers

Use QLabel in Qt Creator Then Go to Properties of QLabel Go to Pixmap Select the file

Its done.

like image 133
arslanekhan Avatar answered Oct 03 '22 20:10

arslanekhan


You can create a QWidget or a QFrame at the place you want to add an image. You can then use the style sheet to set a background-image on that picture. You might want to add your image to your ressources (.qrc).

Using QtDesigner is a good idea for this kind of task.

EDIT : Here is an easy way to do that without bothering with QPixMap.

QWidget *frame = new QWidget(this);
frame->setGeometry(x, y, width, height);
frame->setStyleSheet("background-image: url(:/path/to/image.png)");

The : specifies that you want to use the path in your Qt resources. The first two lines are not needed if you define this QWidget using QtDesigner.

Also, don't forget to import your resources.qrc (however you called it) file (including your resources) and add this to your .pro :

RESOURCES     = resources.qrc
like image 30
wrousseau Avatar answered Oct 03 '22 19:10

wrousseau