QML opening same QML window RAM issue



  • I am using QT 5.2.1 on Ubuntu 14.04.

    I have created full screen application with 2 pages. Each page is created using 2 different qml files.
    I am switching between qml file using following commands

    var component = Qt.createComponent("qrc:///qml/qml/Page2.qml")
    var window    = component.createObject(rootWindow)
    window.showFullScreen()
    rootWindow.visible = false
    

    where rootWindow is ApplicationWindow of Page1.qml

    ApplicationWindow {
        id: rootWindow
    ..
    ..
    }
    

    And using same format in Page2.qml.
    I am switching between two qml on keypress event.
    I switching works perfectly.

    But the issue is that whenever i switch the qml page everytime new memory is allocated for that page in RAM. After several switching the RAM of my PC is full and PC is hanged.

    So How can clear ram of currently oped window after/before switching to the new page.?
    Or any other way that i can use previously assigned memory for each page instead of assigning new memory.


  • Moderators

    @RiteshPanchal

    But the issue is that whenever i switch the qml page everytime new memory is allocated for that page in RAM...

    Are you sure you are not creating the component everytime you switch from one to another ?



  • Ya i am creating new component every switch.
    And i know that's the reason that why new ram is assigned.

    But how can i switch without creating the new component and referring the old one.?


  • Moderators

    @RiteshPanchal hide and show on switch



  • @p3c0 can you show some code snippet?

    Whenever i switch back to current qml all variable or component created new without preventing the old values


  • Moderators

    @RiteshPanchal From where you are creating these 2 pages you have access to its object. Use them to hide/show. You have already done it:
    rootWindow.visible = false

    May be you are not properly doing it.
    Can you post a minimal complete example ?



  • Please find my code
    https://drive.google.com/open?id=0B8d7zQv-G71oMHJjcFBuTnNHTUE

    Thanks in advance.


  • Moderators

    @RiteshPanchal So it seems you are creating and loading the Window from one another on keypress. I would suggest you to use a seperate main window which will create and load these 2 (Menu.qml and Startupq.ml) windows. Then on keypress from either of these QML send a signal to the main window to hide/show the other.



  • @p3c0 How to send signal to main window ?

    And by main windows you are referring some qml file like main_window.qml right?


  • Moderators

    @RiteshPanchal

    How to send signal to main window ?

    Refer the following:
    http://doc.qt.io/qt-5/qtqml-syntax-signals.html#connecting-signals-to-methods-and-signals
    http://doc.qt.io/qt-5/qtqml-syntax-signals.html#adding-signals-to-custom-qml-types

    And by main windows you are referring some qml file like main_window.qml right?

    That is just a name. It can be anything but convention is to use first letter as upper case.
    So somthing like

    //MainWindow.qml
    Window {
        id: root
        visible: true
    
        //Then here after create and instantiate the other 2 items when required.
    }
    


  • Thanks for your reply.

    It seems complicated for me but I will try this method.
    Actually i am new to Qt and Qml so i am facing the difficulties.

    I am use single qml file and create different rectangles for each page there will be no memory issue. I just need to do visible=false for current rectangle and visible=true for new rectangle.

    But as my code become large it will difficult to review and edit. So i am using different qml files for different pages.
    As my application is Full Screen i am using ApplicationWindow instead of just rectangle.

    So is it possible i just create one MainWindow as application window and other pages as just rectangles in different qml files. And show/hide any qml file rectangle from main application window?


  • Moderators

    @RiteshPanchal

    So is it possible i just create one MainWindow as application window and other pages as just rectangles in different qml files. And show/hide any qml file rectangle from main application window?

    Yes.

    Usually this is only one main window. This will be your ApplicationWindow. Then from here you will launch multiple QML files whose root item better be Item. Launching them from this window will allow you keep a track of launched instances from main window.



  • Thanks for the Reply.

    @p3c0 said in QML opening same QML window RAM issue:

    Usually this is only one main window. This will be your ApplicationWindow. Then from here you will launch multiple QML files whose root item better be Item. Launching them from this window will allow you keep a track of launched instances from main window.

    Can you show me some snippet or example code link?
    It would be very helpful


  • Moderators

    @RiteshPanchal Ok. Here is a very simplified demo which may help you understand the basics. Once you get familiar with these you can explore other options like StackView or SwipeView which also acts as a container for pages and provides methods for its manipulation.
    The following example consider Item as root element for the child components. You can change it to Window or whatever and adding its related small changes.

    //Main window
    //RootWindow.qml
    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import QtQuick.Window 2.1
    
    Window {
        id: root
        width: 250
        height: 250
    
        property QtObject obj1
        property QtObject obj2
    
        signal hideObj(QtObject obj)
    
        Component.onCompleted: {
            root.hideObj.connect(onHideObj)
        }
    
        function onHideObj(obj) {
            obj.visible = !obj.visible
        }
    
        Row {
            anchors.top: parent.top
            Button {
                text: "One"
                onClicked: {
                    if(!obj1) {
                        obj1 = Qt.createComponent("Item1.qml").createObject(root);
                    }
                    obj1.visible = true;
                }
            }
    
            Button {
                text: "Two"
                onClicked: {
                    if(!obj2) {
                        obj2 = Qt.createComponent("Item2.qml").createObject(root);
                    }
                    obj2.visible = true;
                }
            }
        }
    }
    
    //Item1.qml
    import QtQuick 2.6
    
    Item {
        id: item1
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        width: 50
        height: 50
    
        Rectangle {
            color: "red"
            anchors.fill: parent
            Text {
                anchors.centerIn: parent
                text: "Item1"
            }
    
            MouseArea {
                anchors.fill: parent
                onClicked: root.hideObj(obj2)
            }
        }
    }
    
    //Item2.qml
    import QtQuick 2.6
    
    Item {
        id: item2
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        width: 50
        height: 50
    
        Rectangle {
            color: "green"
            anchors.fill: parent
            Text {
                anchors.centerIn: parent
                text: "Item2"
            }
    
            MouseArea {
                anchors.fill: parent
                onClicked: root.hideObj(obj1)
            }
        }
    }
    

    The two buttons here creates and shows 1 item each containing a colored rectangle and a text displayed at the bottom. Then after creating these 2 items, try clicking on each individual colored rectangle, it will hide/show other rectangle. This works by sending a signal to the root window from the child component.


Log in to reply
 

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