qt - 弹出项目失去不透明度

标签 qt qml qt5

我对 QtQuick2 的 Popup QML 组件有一个奇怪的问题,当我open()它时,它显示透明背景,它应该是不透明的。

这就是我从 main.qml 文件中调用 Popup 组件的方法:

NewUser {
    id: new_user_form
}

这是组件的来源:

// File: NewUser.qml
Popup {
    id: new_user_popup
    modal: true
    focus: true
    x: 10
    y: 10
    width: 300
    height: 200


    Rectangle {
        anchors.fill: parent
        color: "transparent"
        border.color: "red"
    }
}

这是输出:

Transparent Popup

现在,我可以通过将 NewUser.qml 的源代码移至 main.qml 来修复此错误,现在一切看起来都很好:

这是现在“已修复”的main.qml:

Popup {
    id: new_user_form
    modal: true
    focus: true
    x: 10
    y: 10
    width: 300
    height: 200
}

看,弹出窗口完全不透明:

enter image description here

那么,为什么如果我将组件的源代码从 main.qml 移动到一个单独的文件中,它会失去不透明性?我的main.qml还有很多其他的东西,但我相信它与弹出窗口无关,而且ID是唯一的。我希望这不是 QT 5.8 RC(我用于开发)的问题,它还不是正式的,但很快就会成为正式的。

编辑:

我相信我发现了一个错误。该错误可在 Qt 5.8 和 Qt 5.7 中重现

要重现,请使用以下文件创建一个项目:

主文件:

//main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    NewUser {
        id: new_user_form
    }
    Button {
        text: "open popup"
        onClicked:  {
            new_user_form.open()
        }
    }
}

组件文件(NewUser.qml)

//File: NewUser.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import QtQuick.Templates 2.0

Popup {
    id: new_user_popup
    modal: true
    focus: true
    x: 10; y:10;
    height: 200; width: 300;

    Button {
        text: "Test button"
    }
}


//File: qtquickcontrols2.conf
; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html

[Controls]
Style=Material

[Universal]
Theme=Light
;Accent=Steel

[Material]
Theme=Light
;Accent=BlueGrey
;Primary=BlueGray



//File: main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}

这个 bug 是这么做的吗?如果您单击“打开弹出窗口”按钮,您将看到一个变暗的屏幕,但根本没有弹出窗口。

最佳答案

这不是一个错误。请注意,您没有使用命名空间来导入模板。

import QtQuick.Templates 2.0 as T

为了清楚起见,QtQuick.TemplatesQtQuick.Controls 导入提供的类型之间存在一对一的映射。对于 QtQuick.Controls 导入中可用的每种类型,QtQuick.Templates 导入中都存在同名的非可视模板类型。建议为模板导入使用命名空间,以避免与 QtQuick.Controls 导入提供的类型重叠。

关于qt - 弹出项目失去不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41526665/

相关文章:

c++ - 不同的QT widgets只获取一个QOpenGLContext

qt - QML中的if语句

c++ - Qt - 简单的 Http 服务器

Blackberry 10 Cascades - 文本区域内的图像

c++ - 在 QObject 子类中存储一对

c++ - Qt5-QML : How to dynamically erase objects using a Button

qt - 多次迭代后不断生成 QPixmap* 失败

c++ - 未定义的 vtable 引用,Linux 中的 Qt

c++ - 我的 QTreeWidgetIcons 在哪里?

c++ - Qt Json十进制值