QML Lifecycle



  • I have a main window, and it has the menu to show a typical 'About..' window.

    ApplicationWindow {
        id: main
        title: qsTr("Hello World")
        width: 640
        height: 480
        visible: true
    
        menuBar: MenuBar {
            Menu {
                title: qsTr("mac")
                MenuItem {
                    text: qsTr("about")
                    onTriggered: {
                        var component = Qt.createComponent("About.qml");
                        var about = component.createObject(main);
                        about.show();
                    }
                }
    ...
    
    1. How to prevent that a user opens the same window twice? Do I need a flag variable in C++ code or is it easily done in QML? This seems a common workflow (preference window, open/save dialog, etc). I don't want to disable the main window while About window is open.

    2. If I keep an instance of About window as an attribute of main ApplicationWindow, does it occupy the memory since the main window is loaded? If I have many different windows, this is really inefficient. What's the simplest solution?


  • Moderators

    @emppu

    How to prevent that a user opens the same window twice? Do I need a flag variable in C++ code or is it easily done in QML? This seems a common workflow (preference window, open/save dialog, etc). I don't want to disable the main window while About window is open.

    You can do it right itself in QML. Declare component as global property and then check for null before creating that component.

    property Component component
    ...
    MenuItem {
      ...
      onTriggered: {
        if(!component) {
            component = Qt.createComponent("About.qml");
            var about = component.createObject(main);
            about.show();
        }
      }
    }
    

    In my case main window didn't disable. Did you set any particular Qt::WindowFlags flag ?

    If I keep an instance of About window as an attribute of main ApplicationWindow, does it occupy the memory since the main window is loaded? If I have many different windows, this is really inefficient. What's the simplest solution?

    Anything instantiated will occupy memory. To delay that process you use the technique you mentioned or you can use Loader to load them dynamically.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.