QML:矩形的透明度不起作用

发布于 2024-12-11 01:57:38 字数 1288 浏览 0 评论 0原文

如何设置矩形/屏幕的透明度。

我有以下代码:

// main.cpp
void main(int argc, char*[] argv)
{
    QApplication::setGraphicsSystem("raster");
    QApplication app(argc, argv);

    QDeclerativeView view;
    view.setSource(QUrl::fromLocalFile("loaderTest.qml"));

    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.showFullScreen();

    //QRegion mask(10, 10,  100, 100);
    //view.setMask();

    view.show();
    app.exec();
}

QML 文件是:

//loaderTest.qml
Rectangle
{
    id: mainRectangle
    width: 1000
    height: 700
    color: "transparent"
    //color: "#00000000"

    Image
    {
        id: image1;
        width: 348;
        height: 155;
        anchors.horizontalCenter: parent.horizontalCenter;
        anchors.verticalCenter: parent.verticalCenter;
        source: "test.png"
    }

    Loader
    {
        id: mainLoader
        anchors.fill: parent;
        source: "";
        focus: true;
    }
}

我在该屏幕中有一个加载程序和一张图像,背景颜色是透明的。 当我运行这个应用程序时,它应该显示透明背景,图像位于中心(因为我没有设置加载程序源)。

但我得到的是中心图像,屏幕上填充了白色背景,我不知道谁在填充这个白色背景颜色,因为我已经提到透明颜色作为背景。

我使用的是 QT.4.7.0 和 Linux。

我的目标系统上有两个平面,一个是视频平面,另一个是图形平面,当我运行具有透明背景的 GUI(在视频位置设置透明度)时,它应该在视频位置上显示视频,在上面的示例中,它显示背景为白色,如下所示它应该显示在视频平面上播放的视频。

How to set transparency of an rectangle/screen.

I have Following code:

// main.cpp
void main(int argc, char*[] argv)
{
    QApplication::setGraphicsSystem("raster");
    QApplication app(argc, argv);

    QDeclerativeView view;
    view.setSource(QUrl::fromLocalFile("loaderTest.qml"));

    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.showFullScreen();

    //QRegion mask(10, 10,  100, 100);
    //view.setMask();

    view.show();
    app.exec();
}

And QML file is:

//loaderTest.qml
Rectangle
{
    id: mainRectangle
    width: 1000
    height: 700
    color: "transparent"
    //color: "#00000000"

    Image
    {
        id: image1;
        width: 348;
        height: 155;
        anchors.horizontalCenter: parent.horizontalCenter;
        anchors.verticalCenter: parent.verticalCenter;
        source: "test.png"
    }

    Loader
    {
        id: mainLoader
        anchors.fill: parent;
        source: "";
        focus: true;
    }
}

I have one loader and one image in this screen and background color is transparent.
When i run this application it should display transparent background with image in the center (as i have not set loader source).

but what i am getting is image in center with white background filled in the screen, I don’t know who is filling in this white background color as i have mentioned transparent color as background.

I am using QT.4.7.0 and Linux.

I have two planes on my target system one is Video plane and another is graphics plane, when i run GUI with transparent background (set transparency at video place) it should display video on Video place in above example it is showing background as white, as it should have displayed video playing on Video plane.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凝望流年 2024-12-18 01:57:38

默认情况下,QDeclarativeView 绘制背景。也许这就是你的情况的问题。

来自http://doc.qt.nokia.com/4.7/qdeclarativeperformance.html

如果您使用 QML 提供应用程序的背景,您还可以阻止 QDeclarativeView 绘制其窗口背景,例如

 QDeclarativeView window;
 window.setAttribute(Qt::WA_OpaquePaintEvent);
 window.setAttribute(Qt::WA_NoSystemBackground);
 window.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
 window.viewport()->setAttribute(Qt::WA_NoSystemBackground);

By default the QDeclarativeView paints a background. Maybe that's the problem in your case.

From http://doc.qt.nokia.com/4.7/qdeclarativeperformance.html

You can also prevent QDeclarativeView from painting its window background if you will provide the background of your application using QML, e.g.

 QDeclarativeView window;
 window.setAttribute(Qt::WA_OpaquePaintEvent);
 window.setAttribute(Qt::WA_NoSystemBackground);
 window.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
 window.viewport()->setAttribute(Qt::WA_NoSystemBackground);
甜嗑 2024-12-18 01:57:38

我的第一个猜测是背景是白色的,并且您的矩形确实是完全透明的。

LoaderScreen 是什么类型,我猜它是某种 QDecalarativeView,请原谅任何技术失误,因为已经快一年没有编写 Qt/QML 了。我记得视图的默认背景是白色的,您希望实现什么样的透明度?

My first guess is that the background is white and your rectangle is indeed fully transparent.

What type is LoaderScreen, I guess it is some sort of QDecalarativeView, please forgive any technical misses have not coded Qt/QML for almost a year now. As I remember it the default background of the view was white and what kind of transparency are you hoping to achive anyways?

安穩 2024-12-18 01:57:38

如果您只在 QPushButton 上设置背景颜色,则背景可能不会出现,除非您将 border 属性设置为某个值。这是因为,默认情况下,QPushButton 绘制一个与背景颜色完全重叠的本机边框。
这可能会在 QPushButton 周围显示一个黑色矩形框。您将面临这个问题,尤其是在 Linux 机器上。为了避免这种情况,您可以将 border 属性设置为 none

widget->setStyleSheet("border:none");

If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color.
This might show a black rectangular box around QPushButton. You will face this issue especially on Linux machines. In order to avoid this you can set the border property to none

widget->setStyleSheet("border:none");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文