Solved Adjust an image on MainWindow (directory problems)
-
Hello,
I try to adjust an image in the background of Mainwindow, below is my code, I think that it's right, (another better suggestion will be wellcome)... but the image don't show. Could be a path problem? I don't understand what the two points in a path mean. I stored my image in the same folder of my projectthanks in advance
Greetings#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGridLayout> #include <QPalette> #include <QPushButton> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QPalette* palette = new QPalette(); palette->setBrush(QPalette::Background,*(new QBrush(*(new QPixmap("fondo.jpg"))))); setPalette(*palette); // setWindowFlags(Qt::FramelessWindowHint); //sirve para poner ventana sin botones QWidget *centralWidget = new QWidget(this); QGridLayout *layout = new QGridLayout(); centralWidget->setLayout(layout); layout->addWidget(new QPushButton("Button 1"),0,0); layout->addWidget(new QPushButton("Button 2"),0,1); layout->addWidget(new QPushButton("Button 3"),0,2); setCentralWidget(centralWidget); //Prueba 1 funciona regular ui->setupUi(this); setFixedSize(1333,768); }
-
Why all these pointers?!
Why not just:QPalette palette; palette.setBrush(QPalette::Background,QBrush(QPixmap("fondo.jpg")));
You should check whether this QPixmap("fondo.jpg") creates a valid QPixmap. From documentation:
"Constructs a pixmap from the file with the given fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap."Two points in a path means parent directory:
c:\first\second\..\file.txt means (is same as) c:\first\file.txt
-
"I stored my image in the same folder of my project" - the file should be in the same directory as your executable.
-
Hi
In most cases, you would add a qt resource file and add the image to that.
then load using the special syntax ":/"
http://www.bogotobogo.com/Qt/Qt5_Resource_Files.phpIn your case, you ask it to load without any path so it will look where exe file is
or what ever is current directory.If you dont want to use res file. you can use
qDebug() << "App path : " << qApp->applicationDirPath();
That is where the exe is generated. the build folder or later the install folder.
so
QBrush b(Pixmap(qApp->applicationDirPath()+"/"+ "fondo.jpg"));
palette->setBrush(QPalette::Background, b);(ps. you do not need to new brushes or pixmaps most of the times);
psps. put image in build folder with exe .
-
I will try the yout solutions thank you very much.
I was talking about the vertical tho points " :" type ":\project\archive"
Greetings. -
-
I don't know what happens, the image was once show, but no more
:-( -
-
Here is my new code, but do not work, show a black window
fondo.jpg is in the same path like the project directoryMainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{//QPalette* palette = new QPalette(); QPalette palette; palette.setBrush(QPalette::Background,QBrush(QPixmap("fondo.jpg"))); setPalette(palette); ui->setupUi(this); setFixedSize(1333,768);
}
-
please, a tutorial on the use of images and icons in Qt?
-
The image should not be in the project folder, but the object of compilation, in the root, not in debug or relase
-
You should first call ui->setupUi(this); and then do all the other UI related stuff:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //QPalette* palette = new QPalette(); QPalette palette; palette.setBrush(QPalette::Background,QBrush(QPixmap("fondo.jpg"))); setPalette(palette); setFixedSize(1333,768); }