Solved Adjust an image on MainWindow (directory problems)
-
"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); }