gpt4 book ai didi

c++ - Qt- 改变 QPixmap 的不透明度

转载 作者:太空狗 更新时间:2023-10-29 21:39:28 35 4
gpt4 key购买 nike

如何改变QPixmap的不透明度?

我已经将图像设置为背景实际上我想改变它的不透明度,这是我的代码:

调用.h:

private:
QPixmap m_avatar;

调用.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);
}

pixmap case 绘画更新

如果只需要使用 QPainter 在像素图上绘制一些不透明度,则必须仅在通过 QPainter::begin 激活 QPainter 之后设置不透明度()。因此,在更改顺序后,像素图 result_avatar 有两个图像(一个调整大小后不透明度为 1,原始像素图在顶部,不透明度为 0.5):

QPainter painter;
painter.begin(&result_avatar);
painter.setOpacity(0.5);
painter.drawPixmap(0, 0, m_avatar);
painter.end()

关于c++ - Qt- 改变 QPixmap 的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32922561/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com