Results 1 to 12 of 12

Thread: Multi 'page' QML application issues with external QML/Javascript

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2011
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Quote Originally Posted by anda_skoa View Post
    You haven't used any of the elements of that directory in the code you provided so far, so it is hard to tell what could be wrong.
    Fair enough. To get where my UI is updated as detailed below, I was trying to pass the QML to my javascript function. So, I import the SubPages folder and pass the MainWin.qml to my javascript, but that results in the MainWin undefined error:

    Qt Code:
    1. function gpsOneShotUpdate() {
    2. Funcs.gpsOneShotUpdate(MainWin)
    3. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by anda_skoa View Post
    Ah, I remember now. You are trying to create an artifically complex situation. Any specific reason for that?
    Not trying to complicate things...

    This is for use on mobile devices... On startup, my app needs to get the users location (which is done via a gpsio.cpp function). Once that location is determined, the app needs to update the UI with that location. This initial location needs to be determined before the initial 'Main Window' is visible.

    So... C++ gets location and sets main.qml variables. Then C++ calls main.qml function to update UI on SubPage qml. main.qml function in turn calls javascript function which does the real Text object updates on SubPage qml.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Quote Originally Posted by scgrant327 View Post
    Fair enough. To get where my UI is updated as detailed below, I was trying to pass the QML to my javascript function. So, I import the SubPages folder and pass the MainWin.qml to my javascript, but that results in the MainWin undefined error:

    Qt Code:
    1. function gpsOneShotUpdate() {
    2. Funcs.gpsOneShotUpdate(MainWin)
    3. }
    To copy to clipboard, switch view to plain text mode 
    You are not passing MainWin.qml but MainWin.
    Did you want to pass an instance of MainWin?

    I.e. you are trying the C++ equivalent of passing a class name as a function argument.

    Quote Originally Posted by scgrant327 View Post
    Not trying to complicate things...
    Well, exporting the values is far easier than to call into QML.

    Quote Originally Posted by scgrant327 View Post
    This is for use on mobile devices... On startup, my app needs to get the users location (which is done via a gpsio.cpp function). Once that location is determined, the app needs to update the UI with that location. This initial location needs to be determined before the initial 'Main Window' is visible.
    So just gather the value and set it as a context property before loading main.qml?

    Quote Originally Posted by scgrant327 View Post
    So... C++ gets location and sets main.qml variables.
    Which is not jsut very uncommon (creating a C++ -> QML dependency instead of just the other way around), it is also more complicated.

    Quote Originally Posted by scgrant327 View Post
    Then C++ calls main.qml function to update UI on SubPage qml. main.qml function in turn calls javascript function which does the real Text object updates on SubPage qml.
    Quite complicated, isn't it?

    Even if values would change after loading the QML, exposing them via properties on an object would still be easier by far.

    The only thing that it is not so easy is to get the equivalent of the return value.
    But you don't seem to either return something from the function nor do you try to use the returned value. Quite puzzling why you even retrieve it.

    Cheers,
    _

  3. #3
    Join Date
    May 2011
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Did you want to pass an instance of MainWin?
    Yes! I guess?

    So just gather the value and set it as a context property before loading main.qml?
    Going to mod my code and load main.qml (and additionally MainWin.qml inside main.qml), and then use the gpsio function to get and set the values. I am already exposing the necessary variables from main.qml to C++, so the gpsio function can update them as needed.

    Which is not jsut very uncommon (creating a C++ -> QML dependency instead of just the other way around), it is also more complicated.
    ...and 100% necessary. In the end, there will be a gpsio function that continuously updates the QML UI with new values.

    The only thing that it is not so easy is to get the equivalent of the return value.
    But you don't seem to either return something from the function nor do you try to use the returned value. Quite puzzling why you even retrieve it.
    The return value is not necessary. It is left over from me just trying to get everything to work....and copy/pasting code from online examples. The return value can and will be deleted.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Quote Originally Posted by scgrant327 View Post
    Yes! I guess?
    Then you need to pass the id of the instance or the name of the property holding the instance.
    In C++ you would also pass the name of the variable that hold the pointer to the object, not the class name, right?

    Quote Originally Posted by scgrant327 View Post
    I am already exposing the necessary variables from main.qml to C++, so the gpsio function can update them as needed.
    I was talking about exposing the C++ values to QML.

    Quote Originally Posted by scgrant327 View Post
    ...and 100% necessary. In the end, there will be a gpsio function that continuously updates the QML UI with new values.
    Not necessary at all, that is what property bindings are for.

    Quote Originally Posted by scgrant327 View Post
    The return value is not necessary. It is left over from me just trying to get everything to work....and copy/pasting code from online examples. The return value can and will be deleted.
    I see.

    Then it is really unnecessary to call a JavaScript function from C++. Getting a return value is basically the only use case for that.

    Cheers,
    _

  5. #5
    Join Date
    May 2011
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Ok...some progress...

    Property bindings look like they will work fine for my purpose.

    Issues:
    When calling my javascript functions, why can I not access my QML objects? I get errors about the object being undefined. I've tried by using the alias for the object, the id for the object and the QML file name that houses the object....all give the same error.

    AND... do I need to 'import' the parent and SubPages folders into each of my SubPages/*.qml files?
    I ask because if I try to reference an object in SubPages/Page1.qml from SubPages/Page2.qml, I get an error about that object being undefined.

    For instance, when I load SubPages/Page2.qml using the loader I showed earlier. Page2 has a Label that when clicked on will change a value on Page1. But, if I try this I get an error:

    In Page2:
    Qt Code:
    1. MouseArea {
    2. anchors.bottom: parent.bottom
    3. anchors.right: parent.right
    4.  
    5. Label {
    6. id: page2Label
    7. text: qsTr("ClickME")
    8. horizontalAlignment: Text.AlignRight
    9. anchors.top: parent.top
    10. anchors.bottom: parent.bottom
    11. anchors.right: parent.right
    12. anchors.left: parent.left
    13. font.pointSize: 24
    14. color: "lime"
    15. }
    16. onClicked: {
    17. Page1.page1Label.text = page2Label.text
    18. Page1.page1Label.color = "lime"
    19. page2Label.color = "darkred"
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Quote Originally Posted by scgrant327 View Post
    Property bindings look like they will work fine for my purpose.
    Excellent, bindings on C++ properties is definitely the way to go.
    Way more elegant than making C++ code dependent on QML code.

    Quote Originally Posted by scgrant327 View Post
    When calling my javascript functions, why can I not access my QML objects? I get errors about the object being undefined. I've tried by using the alias for the object, the id for the object and the QML file name that houses the object....all give the same error.
    Can you show such a function and where the objects are defined?

    For example your gpsOneShotUpdate() function should be fine if there is an object with id mainWin in the same file or a property with the name mainWin in the object the function is in.

    Quote Originally Posted by scgrant327 View Post
    AND... do I need to 'import' the parent and SubPages folders into each of my SubPages/*.qml files?
    import allows you access to types, just like includes in C++.
    The current directory, i.e. the directory the current QML file is in, is always implicitly imported.

    In your case you don't need to import SubPages in main.qml since you are using a Loader, you are not instantiating objects of these types directly in QML code.

    Quote Originally Posted by scgrant327 View Post
    I ask because if I try to reference an object in SubPages/Page1.qml from SubPages/Page2.qml, I get an error about that object being undefined.
    Access to objects has nothing to do with imports.

    Quote Originally Posted by scgrant327 View Post
    For instance, when I load SubPages/Page2.qml using the loader I showed earlier. Page2 has a Label that when clicked on will change a value on Page1. But, if I try this I get an error:

    In Page2:
    Qt Code:
    1. onClicked: {
    2. Page1.page1Label.text = page2Label.text
    3. Page1.page1Label.color = "lime"
    4. page2Label.color = "darkred"
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    Page1 is not a valid id nor a valid property name (both need to start with a lower case letter).

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    scgrant327 (24th February 2016)

  8. #7
    Join Date
    May 2011
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    Ok, I ended up declaring non-visible instances of the SubPages in my main.qml. Works pretty well with my custom 'loader' javascript function to show/hide each page on demand.

    Thanks for the help!

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Multi 'page' QML application issues with external QML/Javascript

    You really like doing things imperatively :-)

    However, you might want to look into doing things declaratively for future projects, it really pays off and makes things easier.

    Cheers,
    _

Similar Threads

  1. Multi-page application with Qt Designer
    By chiruL in forum Newbie
    Replies: 1
    Last Post: 23rd January 2016, 10:08
  2. Webkit open new javascript Page
    By ShapeShiftme in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2012, 08:46
  3. Multi-page application and QStackedWidget
    By Mek82 in forum Qt Programming
    Replies: 10
    Last Post: 31st October 2010, 22:06
  4. multi page Application
    By ilpaso in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2010, 09:36
  5. Replies: 3
    Last Post: 25th February 2010, 08:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.