Kian Ahrabian
Kian Ahrabian

Reputation: 921

QMainWindow does not show Qwidgets background

I have a problem regarding a QWidget (custom made) inside a QMainWindow (custom made). My problem is that when I add my widget to my window as its central widget using setCentralWidget() method, it won't show the widgets background. It's important to show the background correctly. Here is my MyWindow.cpp code:

#include "MyMainWindow.h"

MyMainWindow::MyMainWindow(QWidget * parent, Qt::WindowFlags flag) :
        QMainWindow(parent, flag)
{
    this->setFixedSize(1120, 630);
    menu = new MyMenu(this);
//    setting = new MySetting();
//    tutorial = new MyTutorial();
//    game = new MyGame();
    this->setCentralWidget(menu);
    this->show();
}

MyMainWindow::~MyMainWindow()
{
}

My MyMenu.cpp code:

#include "MyMenu.h"

MyMenu::MyMenu(QWidget *parent, Qt::WindowFlags f) :
        QWidget(parent, f)
{
    this->resize(1120, 630);
    this->set_background();
    this->construct_buttons();
    this->construct_menu();
}

MyMenu::~MyMenu()
{
    delete start;
    delete setting;
    delete tutorial;
    delete exit;
    delete buttons;
    delete logo;
    delete menu;
}

void MyMenu::construct_menu()
{
    menu = new QVBoxLayout(this);
    logo = new QLabel(this);
    QPixmap *pixmap = new QPixmap("/home/kahrabian/ClionProjects/Shooter-AP93UT/Contents/logo.png");
    logo->setPixmap(*pixmap);
    logo->setAlignment(Qt::AlignHCenter);
    menu->addWidget(logo);
    menu->addLayout(buttons);
    delete pixmap;
}

void MyMenu::construct_buttons()
{
    buttons = new QHBoxLayout();
    start = new QPushButton("Start", this);
    buttons->addWidget(start);
    setting = new QPushButton("Setting", this);
    buttons->addWidget(setting);
    tutorial = new QPushButton("Tutorial", this);
    buttons->addWidget(tutorial);
    exit = new QPushButton("Exit", this);
    buttons->addWidget(exit);
}

void MyMenu::set_background()
{
    QPalette *palette = new QPalette();
    palette->setBrush(this->backgroundRole(),QBrush(QImage("/home/kahrabian/ClionProjects/Shooter-AP93UT/Contents/background_menu.jpg")));
    this->setPalette(*palette);
    delete palette;
}

My main.cpp code:

#include <QApplication>
#include "MyMenu.h"
#include "MyMainWindow.h"

int main(int argc, char **argv)
{
    QApplication app (argc, argv);
    MyMainWindow *mainwin = new MyMainWindow();
//    MyMenu *MyMenu = new MyMenu();
//    MyMenu->show();
    return app.exec();
}

Can anyone help me with this problem??

Upvotes: 0

Views: 378

Answers (1)

rsht
rsht

Reputation: 1582

Check this answer.

I would recommend you to use Qt style sheets.

You would need to call something like this:

setStyleSheet("image: url(path/to/background/image.png);");

on your widget.

Also, you might need to implement paintEvent() for widget to accept style sheets.
I'm usually doing it like this:

void MyWidget::paintEvent(QPaintEvent *pe)
{    
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    QWidget::paintEvent(pe);
}

Upvotes: 1

Related Questions