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 !
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.
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.
ui->label->setStyelSheet("image:url(:/1. png); border-image:url(:/2. png);");
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.
Use QLabel in Qt Creator Then Go to Properties of QLabel Go to Pixmap Select the file
Its done.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With