Results 1 to 4 of 4

Thread: How to call a Javascript function defined in a QML file from another QML file?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to call a Javascript function defined in a QML file from another QML file?

    This is what I tried:

    TestB.qml

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle
    4. {
    5. id: myItem
    6. width: 100; height: 100
    7.  
    8. function f ()
    9. {
    10. console.log ("sadsad");
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    TestA.qml

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item
    4. {
    5. width: 100; height: 100
    6.  
    7. Loader
    8. {
    9. id: myLoader
    10. source: "TestB.qml"
    11. }
    12.  
    13. MouseArea
    14. {
    15. anchors.fill: parent
    16.  
    17. onClicked:
    18. {
    19. console.log ("You clicked on tab!");
    20. Connections
    21. {
    22. target: myLoader.f();
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    and the error I get is:
    Qt Code:
    1. TestA.qml:15: TypeError: Object [object Object] has no method 'f'
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call a Javascript function defined in a QML file from another QML file?

    myLoader.item.f()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (17th October 2013)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to call a Javascript function defined in a QML file from another QML file?

    Much grateful to you. That works.

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to call a Javascript function defined in a QML file from another QML file?

    Other then that you could put JavaScript helper functions in external js file and simply include it using import.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Call a JavaScript function from C++ using QtWebkit
    By Rastersoft in forum Qt Programming
    Replies: 9
    Last Post: 14th May 2014, 12:03
  2. Replies: 6
    Last Post: 3rd December 2012, 07:26
  3. Replies: 4
    Last Post: 2nd August 2012, 07:42
  4. How to call cpp file function from qml??
    By naufalahad in forum Qt Quick
    Replies: 5
    Last Post: 30th January 2012, 09:52
  5. Replies: 25
    Last Post: 15th January 2006, 00:53

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.