QMainWindow不显示Qwidgets背景
QMainWindow does not show Qwidgets background
我在QMainWindow
(自定义(中遇到了一个关于QWidget
(自定义(的问题。我的问题是,当我使用setCentralWidget()
方法将我的小部件添加到窗口作为它的中心小部件时,它不会显示小部件的背景。正确显示背景很重要。这是我的MyWindow.cpp
代码:
#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()
{
}
我的MyMenu.cpp
代码:
#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;
}
我的main.cpp
代码:
#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();
}
有人能帮我解决这个问题吗??
检查这个答案。
我建议您使用Qt样式表。
你需要这样称呼:
setStyleSheet("image: url(path/to/background/image.png);");
在您的小部件上。
此外,您可能需要为小部件实现paintEvent()
以接受样式表
我通常这样做:
void MyWidget::paintEvent(QPaintEvent *pe)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QWidget::paintEvent(pe);
}
相关文章:
- 如何在全屏模式下(在OpenGL中)使背景透明
- 使用窗口中的QSlider更改背景音量
- SDL2 透明背景
- 在 Visual C++ 中以编程方式更改按钮的背景图像
- 多重采样背景不显示
- 直接2D用WM_ERASEBKGND擦除背景?
- 在特定时间内更改 QLabel 的背景颜色
- 如何设置与其背景图像大小相对应的窗口大小?
- 更改包含 QButton 的 Qlayout 的背景颜色
- 如何为使用创建窗口创建的窗口创建自定义背景颜色
- 删除K的背景掩码-意味着Python或C++中的集群/
- SystemParametersInfo将背景设置为纯色,而不是实际设置图片
- 背景任务UWP C++只触发一次
- 我正在尝试更改背景的颜色,但它不起作用
- 在 C 中获取背景外壳命令的 PID
- 如何在QT中为QInputDialog输入密码时设置背景非活动和灰色?
- MFC 更改 CListCtrl 背景颜色
- 如何仅设置 QTabWidget 背景颜色样式表
- Qt为所有对话框设置通用背景
- QMainWindow不显示Qwidgets背景