QML opening same QML window RAM issue
-
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.?
-
@RiteshPanchal hide and show on switch
-
@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-G71oMHJjcFBuTnNHTUEThanks in advance.
-
@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.
-
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-typesAnd 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?
-
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 beItem
. 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 -
@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
orSwipeView
which also acts as a container for pages and provides methods for its manipulation.
The following example considerItem
as root element for the child components. You can change it toWindow
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.