如何使用指定的png文件制作QWidget背景? [英] How to make QWidget background with specified png file?
问题描述
MyDialog::MyDialog(QWidget* parent, Qt::WindowFlags f)
: QWidget(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
, _pixmap(new QPixmap(myPngFile))
{
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(*_pixmap));
this->setPalette(palette);
setFixedSize(_pixmap->size());
}
myPngFile 定义 png 路径.问题是当我显示 MyDialog 时,png 文件中的透明部分显示为黑色,我该如何纠正它以加载 myPngFile?
myPngFile define the png path. The problem is the transparent part in png file showed black when I show MyDialog, how do I correct it to load myPngFile?
我使用的是带有 Qt4.8 的 Windows 平台
I am using Windows platform with Qt4.8
请不要使用样式表.
推荐答案
如果你真的不想使用样式表,你的问题可以通过覆盖你的 MyDialog
类的绘制事件来解决就像在这个 stackoverflow 问题的答案中一样:背景图片未在 QWidget 上显示
If you really don't want to use style sheets, your problem could be solved by overwriting the paint event of your MyDialog
class like in the answer of this stackoverflow question:
Background image not showing on QWidget
但我也建议您使用样式表来解决您的问题.
But I would also recommend using style sheets for your problem.
这篇关于如何使用指定的png文件制作QWidget背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!