【问题标题】:Qt: creating semi-transparent disabled icon statesQt:创建半透明的禁用图标状态
【发布时间】:2012-05-31 21:53:36
【问题描述】:

我想从原始图标图像中动态地为我的图标禁用状态创建一个半透明图标像素图(不为禁用状态使用额外的图像)。

我认为这需要五分钟,只需创建一个 QPainter,将其不透明度设置为 0.5 或其他值,然后在其中绘制正常的 Pixmap。

问题是 QPainter 似乎从设置为 (205, 205, 205) 的背景开始,我无法对其进行设置以使其完全透明。

这是我用于标准图标的代码:

icon.addPixmap(QPixmap(filename));

这是我迄今为止尝试为禁用状态制作透明版本的方法:

QPixmap normalPixmap(filename);
QPixmap disabledPixmap(normalPixmap.size());
QPainter p(&disabledPixmap);

p.setBackgroundMode(Qt::TransparentMode);
p.setBackground(QBrush(Qt::transparent));
p.eraseRect(normalPixmap.rect());
// (...) I've tried Composition modes and a lot of other stuff here, with no success

p.setOpacity(0.5);
p.drawPixmap(0, 0, normalPixmap);

p.end();
icon.addPixmap(disabledPixmap, QIcon::Disabled, QIcon::On);

这些是我从上面的代码中得到的结果:

【问题讨论】:

    标签: c++ qt user-interface icons qpainter


    【解决方案1】:

    尝试:

    disabledPixmap.fill(Qt::transparent);

    在创建 QPainter 之前

    【讨论】:

    • 完美!谢谢,我欠你一杯啤酒。
    【解决方案2】:

    将图像绘制到 disabledPixmap 后,尝试以下代码:

    // Make the bitmap partially transparent by setting the alpha channel of all pixels
    painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    painter.fillRect(thePixmap.rect(), QColor(255,255,255,150));
    

    【讨论】:

    • 不,不起作用。图像变暗,但仍无法按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2011-02-22
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多