0

Is it possible to display a QML dialog when the user hits a button in a QML window?

Example:

When the user clicks in the menu bar on Help -> About the About dialog should be displayed:

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

import "components"

ApplicationWindow {
    id: window
    visible: true
    width: 1040
    height: 480


    aboutDlg: aboutDialog {}  // does not work...

    menuBar: MenuBar {
        Menu {
            title: qsTr("Help")
            MenuItem {
                text: qsTr("About")
                onTriggered: aboutDlg.open();
        }
    }
    ...

components/AboutDialog.qml

import QtQuick 2.2
import QtQuick.Dialogs 1.1

MessageDialog {
    id: aboutDialog
    title: "May I have your attention please"
    text: "It's so cool that you are using Qt Quick."
    onAccepted: {
        console.log("And of course you could only agree.")
        Qt.quit()
    }
}

When I remove the line boutDlg: aboutDialog {} // does not work... the following error is reported when clicking on the About menu item:

qrc:/main.qml:61: ReferenceError: aboutDlg is not defined

How can I achieve this?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
  • 1
    More info is [here](https://doc.qt.io/qt-5/qtqml-documents-definetypes.html). Also I advice to start learning QML from [this](https://qmlbook.github.io/) pretty book. – folibis Apr 23 '19 at 19:11

1 Answers1

1

You called "aboutDialog" which is an ID in the AboutDialog. Think of it like you add an object, like adding a Rectangle...which has its own file...and you can "instantiate" it by adding an object like so:

...

ApplicationWindow {

...

    AboutDialog {
        id: aboutDlg
    }

...
}

You can find example HERE

You might also optimize a bit and put the AboutDialog in a Loader.

Jax297
  • 617
  • 1
  • 6
  • 8