【问题标题】:QML: how to show a dialog after window open?QML:打开窗口后如何显示对话框?
【发布时间】:2020-01-24 02:06:03
【问题描述】:

在用户开始使用应用程序之前,我需要向用户显示一个模式对话框。带对话框的应用示例:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.3

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

    Button {
        text: "Show dialog"
        onClicked: profileDialog.open()
    }

    Dialog {
        id: profileDialog
        title: "Select profile"
        ColumnLayout {
            RadioButton {
                checked: true
                text: qsTr("First")
            }
            RadioButton {
                text: qsTr("Second")
            }
            RadioButton {
                text: qsTr("Third")
            }
        }
        standardButtons: StandardButton.Ok
    }

}

此示例显示按钮单击时的对话框。但是我需要在应用程序启动时执行此操作。如何在打开主窗口时显示 profileDialog?也许有一些 afterShow 信号?但我在文档中找不到这样的信号。

【问题讨论】:

    标签: qt qml qt5 qtquick2


    【解决方案1】:

    在这些情况下,应使用Component.onCompleted 信号:

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        // ...
        Component.onCompleted: profileDialog.open()
    }

    【讨论】:

    • 感谢您的解决方案。我找到了另一个解决方案:onVisibleChanged: if (visible) profileDialog.open()。哪个更好:onVisibleChanged 还是 Component.onCompleted?
    • @AndreyEpifantsev 在您当前的情况下,任何人都同样有效,使用我的代码,它只会在第一次创建窗口时执行,在您的情况下,每次窗口可见(例如,如果它隐藏窗口并再次显示信号也会被触发),这取决于你想要什么。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多