2017-05-25 144 views
0

我在Windows上使用Qt5.8。我创建了一个我在QML上渲染的QQucikItem。在updatePaintNode功能我创建一个QImage的(其有一些透明部分),为finalImageQSGTexture上的透明QImage

QSGSimpleTextureNode *node = 0; 
QSGTexture *texture;   
node = static_cast<QSGSimpleTextureNode *>(oldNode); 
if (!node) { 
    node = new QSGSimpleTextureNode(); 
} 

texture = window()->createTextureFromImage(finalImage); 
node->setTexture(texture); 
node->setRect(boundingRect()); 
node->markDirty(QSGNode::DirtyForceUpdate); 
return node; 

所以在QML透明部分是变的黑色代替透明

+0

我发现有一个QImage绘图的问题,不知何故图像的透明部分是黑色转换。 – SourabhKus

回答

0

我所用,而不是ARGB32格式RGB32创建的QImage。

2

我不不知道你在做什么,但是黑色背景意味着图像没有加载。通常这是错误的路径或类似的东西。或者可能是图像损坏。您只能有意识地填充背景,默认情况下它是透明的。

例如这样的代码:

C++:

TestItem::TestItem(QQuickItem *parent) 
    :QQuickItem(parent) 
{ 
    setFlag(QQuickItem::ItemHasContents); 
} 

QSGNode *TestItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData) 
{ 
    QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(oldNode); 
    if (!node) { 
     node = new QSGSimpleTextureNode(); 
     QImage img(":/sunflower.png"); 
     QSGTexture *texture = window()->createTextureFromImage(img); 
     node->setTexture(texture); 
    } 
    node->setRect(boundingRect()); 
    return node; 
} 

QML

import QtQuick 2.7 
import QtQuick.Window 2.0 
import qml.test 1.0 
import QtGraphicalEffects 1.0 

Window { 
    id: mainWindow 
    width: 600 
    height: 400 
    visible: true 

    LinearGradient { 
     anchors.fill: parent 
     start: Qt.point(0, 0) 
     end: Qt.point(0, 200) 
     gradient: Gradient { 
      GradientStop { position: 0.0; color: "lightblue" } 
      GradientStop { position: 1.0; color: "white" } 
     } 
    } 

    TestItem { 
     width: 200 
     height: 200 
     anchors.centerIn: parent 
    } 
} 

产生这样的结果:

enter image description here

该图像取自here,使用GIMP调整到200x200。 项目结构是这样的:

enter image description here

+0

谢谢你的回答@folibis,但在回答问题之前,请仔细阅读问题。我可以加载我的图像,但只有透明部分存在问题。但相同的代码在你的情况下工作正常。 – SourabhKus

+0

嗯......好的,谢谢你告诉我如何正确回答一个问题。 – folibis