Qt- 改变 QPixmap 的不透明度
Qt- Change opacity of QPixmap
如何改变QPixmap的不透明度?
我已经将图像设置为背景,实际上我想改变它的不透明度,这是我的代码:
Call.h:
private:
QPixmap m_avatar;
Call.cpp:
void Call::resizeEvent(QResizeEvent *e)
{
QPalette pal = palette();
pal.setBrush(backgroundRole(), m_avatar.scaled(e->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
setPalette(pal);
}
我已经更改了 resizeEvent 函数,但它不会更改背景的不透明度。
void Call::resizeEvent(QResizeEvent *e)
{
QPixmap result_avatar(m_avatar.size());
result_avatar.fill(Qt::transparent);
QPainter painter;
painter.setOpacity(0.5);
painter.begin(&result_avatar);
painter.drawPixmap(0, 0, m_avatar);
painter.end();
QPalette pal = palette();
pal.setBrush(backgroundRole(), result_avatar.scaled(e->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
setPalette(pal);
}
有什么建议吗?
您没有使用本地 QPainter
对象。根据 QWidget Events:
paintEvent()
is called whenever the widget needs to be repainted.
Every widget displaying custom content must implement it. Painting
using a QPainter
can only take place in a paintEvent()
or a function
called by a paintEvent()
.
这里有效:
void Call::paintEvent(QPaintEvent *)
{
// create a new object scaled to widget size
QPixmap result_avatar = m_avatar.scaled(size());
QPainter painter(this);
painter.setOpacity(0.5);
// use scaled image or if needed not scaled m_avatar
painter.drawPixmap(0, 0, result_avatar);
}
像素图大小写更新
如果只需要使用 QPainter
在像素图上绘制一些不透明度,则必须仅在 QPainter
由 QPainter::begin()
激活后设置不透明度。因此,在更改顺序后,像素图 result_avatar
有两个图像(一个调整大小,不透明度为 1,原始像素图在顶部,不透明度为 0.5):
QPainter painter;
painter.begin(&result_avatar);
painter.setOpacity(0.5);
painter.drawPixmap(0, 0, m_avatar);
painter.end()
如何改变QPixmap的不透明度?
我已经将图像设置为背景,实际上我想改变它的不透明度,这是我的代码:
Call.h:
private:
QPixmap m_avatar;
Call.cpp:
void Call::resizeEvent(QResizeEvent *e)
{
QPalette pal = palette();
pal.setBrush(backgroundRole(), m_avatar.scaled(e->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
setPalette(pal);
}
我已经更改了 resizeEvent 函数,但它不会更改背景的不透明度。
void Call::resizeEvent(QResizeEvent *e)
{
QPixmap result_avatar(m_avatar.size());
result_avatar.fill(Qt::transparent);
QPainter painter;
painter.setOpacity(0.5);
painter.begin(&result_avatar);
painter.drawPixmap(0, 0, m_avatar);
painter.end();
QPalette pal = palette();
pal.setBrush(backgroundRole(), result_avatar.scaled(e->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
setPalette(pal);
}
有什么建议吗?
您没有使用本地 QPainter
对象。根据 QWidget Events:
paintEvent()
is called whenever the widget needs to be repainted. Every widget displaying custom content must implement it. Painting using aQPainter
can only take place in apaintEvent()
or a function called by apaintEvent()
.
这里有效:
void Call::paintEvent(QPaintEvent *)
{
// create a new object scaled to widget size
QPixmap result_avatar = m_avatar.scaled(size());
QPainter painter(this);
painter.setOpacity(0.5);
// use scaled image or if needed not scaled m_avatar
painter.drawPixmap(0, 0, result_avatar);
}
像素图大小写更新
如果只需要使用 QPainter
在像素图上绘制一些不透明度,则必须仅在 QPainter
由 QPainter::begin()
激活后设置不透明度。因此,在更改顺序后,像素图 result_avatar
有两个图像(一个调整大小,不透明度为 1,原始像素图在顶部,不透明度为 0.5):
QPainter painter;
painter.begin(&result_avatar);
painter.setOpacity(0.5);
painter.drawPixmap(0, 0, m_avatar);
painter.end()