Add an image to the MainWindow using code?
-
Hello everybody, hope all is well!
Currently, I am trying to figure out how to add either a QImage or a QPixmap widget to a window in the source code. I am not using any UI design files (form files), as I want to do this without using them. Thus, I am trying to figure out how to do so through code. As of now, this is what I have done:
QImage *img = new QImage("image.file"); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(img);
However, I have had no luck with this. What do I need to do to insert an image (as well as place it in a precise location)? Thank you in advance!
-
@SGaist Thank you! Would this work?
QPixmap *img = new QPixmap("image.file"); QLabel *label = new QLabel(); label->setPixmap(img); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(label);
Or would I need to try something else?
-
Hi
Yes but no need to new the pixmapQPixmap img ("image.file"); QLabel *label = new QLabel(); label->setPixmap(img); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(label);
Do note that using no path to the image wont work well if you have the image in the project folder as when exe runs, the current folder is the build folder so it looks there.
-
@mrjj Thank you.
Here is my code now for the MainWindow ClassMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QPixmap img (":images/appLogo.svg"); QLabel *label = new QLabel(); label->setPixmap(img); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(label); setLayout(layout); }
However, the window is still blank. I tried the adding in the setLayout function, but still had no luck. Is there an addLabel function similar to the addDockWidget function or would I need to do something else?
-
QMainWindow already has a layout. The one that provides all the nice feature like docking, etc.
You can use setCentralWidget on your label.
-
@WesLow
Hi
Is there a reason you are not using UI files ?You need to add layout to central widget then add some layouts to that to divide it into sections and then place the LOGO label etc,
-
You can create a QWidget subclass and reimplement the paintEvent method to draw the image where you want it.
-
@mrjj Thank you. Yes, it's a weird requirement from my teacher. UI files would make this much easier haha. Also thank you for that example, my thinking is kind of similar to yours there.
@SGaist Is there a good example of this on QT's website; I'm not really sure what you mean by what you're saying.
Thank you both for your help, it is very much appreciated.
-
Hi
- Yes, it's a weird requirement from my teacher.
Ok... His class his rules. .. but you can cheat a bit if you want. Using UI files just generate
code. So you can draw your layout and then steal the code from the setupUI function
or at least be inspired. -
@WesLow
well make a new default gui project using the wizardThen its in MainWindow
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this); <<<<You can also run tool directly on a UI file
https://doc.qt.io/qt-5/uic.htmlbut use a default project is most likely easier.