【问题标题】:Text in fading area turns black淡入淡出区域的文字变黑
【发布时间】:2015-06-26 20:58:40
【问题描述】:

我的 qml 弹出通知窗口代码(期望上下文参数 owner(对象)、messageText、messageTitle 和 messageTimeout)如下:

import QtQuick 2.4
import QtQuick.Window 2.0
import QtQuick.Controls 1.2

Window {
    id: notification
    x: Screen.width - width - 40
    y: 40
    width: 350
    height: background.height
    color: "transparent"
    flags: Qt.SplashScreen | Qt.WindowStaysOnTopHint

    Component.onCompleted: visible = true

    signal closeRequested

    Connections {
        target: owner
        onInitClose: {
            if (closeInitialized) {
                return
            }

            fadeTimer.stop()
            fadeOutAnimation.start()
            closeTimer.start()
        }
    }

    property int fadeoutDuration: 1000
    property bool closeInitialized: false

    NumberAnimation {
        id: fadeOutAnimation
        target: background
        property: "opacity"
        duration: fadeoutDuration
        easing.type: Easing.InOutQuad
        from: 1
        to: 0
    }

    Rectangle {
        id: background
        radius: 5
        color: "#D8226222"

        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        height: childrenRect.height + 20

        Label {
            id: caption
            color: "white"
            font.pixelSize: 18
            wrapMode: "WordWrap";

            text: messageTitle

            anchors.left: parent.left
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.topMargin: 10
            anchors.leftMargin: 15
            anchors.rightMargin: 15
        }

        Label {
            color: "white"
            font.pixelSize: 14
            wrapMode: "WordWrap";

            text: messageText

            anchors.top: caption.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.topMargin: 10
            anchors.leftMargin: 15
            anchors.rightMargin: 15
        }
    }

    Timer {
        id: fadeTimer
        interval: messageTimeout
        running: true
        repeat: false

        onTriggered: {
            closeInitialized = true
            fadeOutAnimation.start()
            closeTimer.start()
        }
    }

    Timer {
        id: closeTimer
        interval: fadeoutDuration
        running: false
        repeat: false

        onTriggered: {
            notification.close()
            owner.onClosed()
        }
    }
}

所以我们的想法是,我们用两个标签显示由半透明区域组成的窗口,启动计时器,当触发时开始淡出(不透明度从 1 到 0)动画并启动另一个计时器(这次是关闭窗口那么褪色就完成了)。

问题是 - 当背景区域褪色时,文本颜色会从白色变为(看似不透明)黑色,尽管文本位于应该变得绝对透明的容器中。我该如何解决这个问题?

所有者cpp类代码

#include "popup.h"

#include <QQmlContext>

Popup::Popup(QString title, QString text, int delay, QObject *parent) : QObject(parent), m_engine(this)
{
    m_engine.rootContext()->setContextProperty("messageTitle", QVariant(title));
    m_engine.rootContext()->setContextProperty("messageText", QVariant(text));
    m_engine.rootContext()->setContextProperty("messageTimeout", QVariant(delay));
    m_engine.rootContext()->setContextProperty("owner", this);
    m_engine.load(QUrl(QStringLiteral("qrc:/popup.qml")));

    m_state = STATE_ACTIVE;
}

bool Popup::isOpen()
{
    return m_state == STATE_ACTIVE;
}

void Popup::onClosed()
{
    m_state = STATE_CLOSED;
    emit closed();
}

void Popup::close()
{
    if (!isOpen()) {
        return;
    }

    emit initClose();
}

【问题讨论】:

  • 你使用什么操作系统/WM?
  • @folibis 我在 Ubuntu 15.04、windows 10 (kvm) 和两次安装的 windows 8.1 (not wm) 中复制了它
  • 你能提供一个bug的zip吗,我很乐意帮助你:)
  • 我怀疑这是显卡驱动问题,因为它在 openSUSE VM 上通过软件光栅运行正常。
  • 我同意@cmannett85:我已经测试了代码,在我的Win7机器上Rectangle和Text变黑然后消失,即opacity不能在全部。不过,无法在我的 Mac 书上进行测试。

标签: qt qml qt5


【解决方案1】:

可以通过在背景中添加“layer.enabled: true”来修复

Rectangle {
    id: background
    layer.enabled: true
    ...
}

【讨论】:

  • 我知道这一定是一些完全随机的事情,没有在任何地方提及...无论如何,感谢并祝贺代表提升! :)
猜你喜欢
  • 2022-06-13
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多