透明小部件与QtQuick 2.0 [英] Transparent widget with QtQuick 2.0
问题描述
我试图用QtQuick 2.0创建一个透明的窗口。
I'am trying to create a transparent window with QtQuick 2.0.
我可以创建一个透明的窗口部件:
I can create a transparent widget like that:
class TransparentWidget : public QWidget
{
public:
TransparentWidget(QWidget* parent)
: QWidget(parent)
{
resize(QSize(500, 500));
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);
}
void paintEvent(QPaintEvent * event)
{
QPainter painter;
QBrush brush(Qt::cyan);
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawRect(0,0,100,100);
painter.end();
}
};
现在我想用QQuickView做同样的事情,首先我创建它:
Now i want to do the same thing with QQuickView, first i create it:
QQuickView view;
view.setSource(QUrl::fromLocalFile("test.qml"));
view.setResizeMode(QQuickView::SizeRootObjectToView);
这里是我的test.qml文件:
and here is my "test.qml" file:
Rectangle
{
width: 300;
height: 300;
color: "transparent";
Rectangle
{
anchors.top: parent.top;
anchors.left: parent.left;
width: 50;
height: 100;
color: "lightsteelblue";
}
Rectangle
{
anchors.bottom: parent.bottom;
anchors.right: parent.right;
width: 50;
height: 100;
color: "darkgrey";
}
}
现在我想创建一个透明的窗口部件它像这样:
now I want to create a transparent widget and i did it like that:
QWidget *p = QWidget::createWindowContainer(&view, NULL, Qt::FramelessWindowHint);
p->setAttribute(Qt::WA_TranslucentBackground);
p->show();
我使用WA_TranseculentBackground和FramelessWindowHint创建一个窗口部件,就像我和前一个窗体一样,但是它没有't work。
i create a widget with WA_TranseculentBackground and FramelessWindowHint just like the way i did with the previous one but it didn't work.
比我更深一点,使用pix看看什么是QQuickView调用后面,我看到这一行:
Than go a little deeper and used pix to see whats QQuickView calling behind and i see this line:
现在我的问题:
1 - 为什么Qt调用IDirect3DDevice9 :: clear颜色为白色?
1 - Why Qt is calling IDirect3DDevice9::clear with color white?
2-有没有办法告诉QQuickView或QmlEngine不绘制任何背景?
2- Is there a way to tell QQuickView or QmlEngine not to draw background anything?
推荐答案
QtQuick2的透明度自Qt 5.2起,至少对于Windows(包括Angle和OpenGL版本):
Transparency with QtQuick2 works since Qt 5.2, at least for Windows (both Angle and OpenGL builds):
创建视图:
QQuickView view;
view.setOpacity(0.9999);
view.setColor( Qt::transparent );
view.setSource( QUrl::fromLocalFile( QLatin1String( "Main.qml" ) ) );
除setOpacity和setColor外,不需要额外的样式/属性/标志调用。
Main.qml必须正确设置透明度,像您的QML示例一样:
No additional style/attribute/flag calls are needed besides setOpacity and setColor. "Main.qml" must set transparency properly, like your QML example also does:
Rectangle {
color: "transparent"
}
这篇关于透明小部件与QtQuick 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!