• May 02, 2024, 02:47:59 am

Author Topic: Dynamic loading in QtQuick  (Read 3067 times)

0 Members and 1 Guest are viewing this topic.

Offline huellif

  • Developer
  • Christmas Santa
  • ****
  • Posts: 402
  • Reputation: 212
Dynamic loading in QtQuick
« on: December 17, 2013, 01:51:43 am »
QtQuick can be a slow thing :P
So you should try to increase the speed.
Most beginners put all dialogs, menus etc. into page.qml file, but you should load them dynamically.

Some people use this way:
(mainPage is the id of your page)
Code: [Select]
property Component __a: null
onClicked:{
if (!__Selector_widgets_classic) __a = Qt.createComponent("dialog.qml")
__a.createObject(mainPage)}}

a loader or something similar.

but all this Component properties and the repeating code slows down QtQuick.
Try this way instead (in your main.qml)

Code: [Select]
QtObject{
id:dialog;
property Component c:null;
function create(qmlfile){
c=Qt.createComponent(qmlfile);
c.createObject(mainPage)
}
}

now you can replace all loaders, components properties etc. in your whole project with:
Code: [Select]
dialog.create(dialog.qm)
(pages should load via PageStack and NOT via dynamic components) 

Offline symster

  • Developer
  • Happy Member
  • ****
  • Posts: 214
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : Nokia N8 Soon Nokia 808
Re: Dynamic loading in QtQuick
« Reply #1 on: December 17, 2013, 06:17:30 pm »
In last line code you mean if i want to open query dialog,instead of using querydialog.open() i need to use dialog.create(querydialog.qml)
« Last Edit: December 17, 2013, 06:21:17 pm by symster »

Offline Allstar12345

  • Allstar Software Founder
  • Administrator
  • Forum Genius
  • ******
  • Posts: 5,235
  • Reputation: 812
    • Allstar Software
  • Current Phone: : OnePlus 8 Pro, Xperia 10, Nexus 6p, Jolla Phone, Nokia N8, Nokia 808 PureView, BlackBerry Z30
Re: Dynamic loading in QtQuick
« Reply #2 on: December 17, 2013, 07:05:14 pm »
In last line code you mean if i want to open query dialog,instead of using querydialog.open() i need to use dialog.create(querydialog.qml)

Basically yeah.

But make sure that Dialog has
Code: [Select]
component.onCompleted:{open();} in it's code.

Offline symster

  • Developer
  • Happy Member
  • ****
  • Posts: 214
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : Nokia N8 Soon Nokia 808
Re: Dynamic loading in QtQuick
« Reply #3 on: December 17, 2013, 08:41:08 pm »
 Err..."basically" confuse me but thanks anyway. :)

Offline huellif

  • Developer
  • Christmas Santa
  • ****
  • Posts: 402
  • Reputation: 212
Re: Dynamic loading in QtQuick
« Reply #4 on: December 19, 2013, 04:57:19 am »
here's an example:
Code: [Select]
import QtQuick 1.1
import com.nokia.symbian 1.1
QueryDialog{
id:aboutDialog
titleText:qsTr("About")
icon:"images/note_info.svg"
message:qsTr("Delight App v1.2.1\ncreated by Fabian\n\nthanks for your help with Qt/QML:\n- Paul\n- Anand\n- Dickson\n- Stefan\n- Maciej\n- Lucas\n\nthanks Marco for your help with native C++\n\nthanks Abdoul for your icons\n\nthanks for (your help with) MiniCMD:\n- Just Fancy\n\- Ashok\n- Eric\n- xCape\n- Marco")
acceptButtonText:qsTr("Back")
onAccepted:aboutDialog.destroy()
Component.onCompleted:open()}

if you have dialogs which can close without of buttons like (context)menus try this:
Code: [Select]
import QtQuick 1.1
import com.nokia.symbian 1.1
ContextMenu{
id:subMenu2
property bool __isClosing: false
MenuLayout{
MenuItem{
text:qsTr("Disable Application caching")
onClicked:dialog.create("Selector_cache_off.qml")}
MenuItem{
text:qsTr("Enable Contacts&Messaging")
onClicked:dialog.create("Selector_cache_some.qml")}}
Component.onCompleted:open()
onStatusChanged:{
if (status === DialogStatus.Closing) __isClosing = true
else if (status === DialogStatus.Closed && __isClosing) subMenu2.destroy()}}

credit for this way of dynamic deleting goes to Dickson

But make sure that Dialog has
Code: [Select]
component.onCompleted:{open();} in it's code.
{;} isn't needed.
Because of the slow javascript engine (compared to QtQuick 2), QDeclertive's parser, the limited ram and heap size, impact on size of your .exe (if you use QRC) less code is better
« Last Edit: December 19, 2013, 05:21:59 am by huellif »

Offline Motaz

  • Developer
  • Forum User
  • ****
  • Posts: 62
  • Reputation: 72
  • Symbian Power User
  • Current Phone: : Nokia 700
Re: Dynamic loading in QtQuick
« Reply #5 on: December 19, 2013, 09:48:24 pm »
Thank you huellif :)

I prefer to use this code to destroy the Dialog (It will keep the close animation  ;)):
Code: [Select]
Component.onCompleted: {
   open()
   isCreated = true
}

property bool isCreated: false
onStatusChanged: {
   if (isCreated && status === DialogStatus.Closed) {
       <DialogID>.destroy()
   }
}

As an alternative to this code:
Code: [Select]
onAccepted:aboutDialog.destroy()
« Last Edit: December 29, 2013, 11:54:19 am by Motaz »

Offline huellif

  • Developer
  • Christmas Santa
  • ****
  • Posts: 402
  • Reputation: 212
Re: Dynamic loading in QtQuick
« Reply #6 on: December 19, 2013, 11:49:03 pm »
Thank you huellif :)

;)

I prefer to use this code to destroy the Dialog (It will keep the close animation  ;)):
Code: [Select]
onStatusChanged: {
    if (status === DialogStatus.Closed)
        destroy()
}

that's a nice way :)


As an alternative to this code:
Code: [Select]
onAccepted:aboutDialog.destroy()

that's a easiest way of course :D
But (context) menus need the "onStatusChanged:"