2017-05-25 62 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