我不不知道你在做什么,但是黑色背景意味着图像没有加载。通常这是错误的路径或类似的东西。或者可能是图像损坏。您只能有意识地填充背景,默认情况下它是透明的。
例如这样的代码:
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
}
}
产生这样的结果:
该图像取自here,使用GIMP调整到200x200。 项目结构是这样的:
我发现有一个QImage绘图的问题,不知何故图像的透明部分是黑色转换。 – SourabhKus