Solved How to add a Backrgound Image on my QMainWindow
-
Hi! Im new to Qt and Im struggling on how to do this,
I tried two methods but none of them worked.
The methods are here:
(I don't use them directly in the QMainWindow constructor because I don't want the background to be definitive in my window)
Thank you for your help!! -
@Victor-Delattre
Please paste code, not show screenshots.For background palette I think you need to set
setAutoFillBackground(true)
on any widget using it.For style sheet, you are using a rule for
MainWindow
, but you don't have any widget with that name.QMainWindow
orMainMenu
might work for you as those are your classes. -
@JonB Hi thank you for your answers!
I tried them but I still have the problem.
WIth Palette now it changes the background (was default grey now it's black) but I don't have my background image.
WIth StyleSheet either with QMainWindow or MainMenu it does nothing.
Pasting the code here:
On main:int main(int argc, char *argv[]) { QApplication app(argc, argv); MainMenu Fenetre_Principale; Fenetre_Principale.lancerJeu(); return app.exec(); }
On MainMenu.cpp:
MainMenu::MainMenu(QWidget *parent) : QMainWindow(parent) { setFixedSize(largeur, hauteur); setWindowTitle("Badlands Cities"); setWindowIcon(QIcon(":/Images/icon.jpeg")); }
void MainMenu::lancerJeu() { Bouton_Play = new CustomButton("Jouer", this); Bouton_Play->setDefault(true); Bouton_Play->move(pour_larg*10, pour_haut*25); Bouton_Quit = new CustomButton("Quitter", this); Bouton_Quit->move(pour_larg*10, pour_haut*50); //First Method with QPalette: /* setAutoFillBackground(true); QPixmap bkgnd(":/Images/MenuBackground.jpeg"); bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio); QPalette pal; pal.setBrush(QPalette::Window, bkgnd); setPalette(pal); */ //Second Method with StyleSheet: /* QString Sheet = R"( MainMenu { border-image: url(":/Images/MenuBackground.jpeg") 0 0 0 0 stretch stretch; })"; setStyleSheet(Sheet); */ //Or: /* QString Sheet = R"( background-image: url(":/Images/MenuBackground.jpeg"); background-position: center; )" CentralWidget()->setStyleSheet(Sheet); */ this->show(); QObject::connect(Bouton_Quit, SIGNAL(clicked()), qApp, SLOT(quit())); QObject::connect(Bouton_Play, SIGNAL(clicked()), this, SLOT(lancerMenu())); }
-
@Victor-Delattre Can someone help me?
All I want is to use an image on the background of my window ! -
Does you app correctly show the window icon you've set?
I feel like it didn't load the jpeg picture successfully.After
QPixmap bkgnd(":/Images/MenuBackground.jpeg");
check the value of
bkgnd.isNull()
. -
@Victor-Delattre the palette version works just fine for me
-
@Bonnie
Yes my Icon is correctly displayed,
I don't know how to print the bkgnd.isNull() value but I understand what you were talking about.
What do I have to do to get access to this value in Qt Creator?
Btw I will try to change the image that is loaded, maybe it doesn't fit with Qt.
Thanks for your answer ! -
@Bonnie Alright!
When I change my Background Image it works!
Thanks for your answers all!