Results 1 to 3 of 3

Thread: How to call a javascript function without the trigger of any event in a grid?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to call a javascript function without the trigger of any event in a grid?

    Below is the given code snippet of the qml file which is loaded through a cpp file of a qt project. In this i want to directly call a .js file fucntion createGrid to run on PowerCell elements in a loop which are commented to create a grid.

    This gives the error: Invalid import for import line of .js file

    Qt Code:
    1. import Qt 4.7
    2. import "loop.js" as genList
    3.  
    4. Rectangle {
    5. id:page
    6. width: 550; height:159
    7. x:0
    8. y:0
    9. signal gridEntry
    10. property variant listOfPowerHogs: powerHogList
    11. Image {
    12. id: backGround
    13. source: "ui_images/background.png"
    14.  
    15. Flickable{
    16. id:flick
    17. anchors.fill: parent
    18. contentWidth: flickArea.width
    19. contentHeight: flickArea.height
    20. flickableDirection: Flickable.HorizontalFlick
    21. Grid{
    22. id:flickArea
    23. x:0
    24. y:12
    25. width: 697
    26. height: 135
    27. rows: 1; columns: 5; spacing: 8;
    28. // PowerCell {
    29. // width:/* page.inPortrait ? (parent.width-4)/2 : (parent.width-8)/3*/ (parent.width-100)/4
    30. // iconValue: listOfPowerHogs[0]
    31. // instrumentedStatus: listOfPowerHogs[1]
    32. // }
    33. // PowerCell {
    34. // width: (parent.width-100)/4
    35. // iconValue: listOfPowerHogs[2]
    36. // instrumentedStatus: listOfPowerHogs[3]
    37. // }
    38. // PowerCell {
    39. // width: (parent.width-100)/4
    40. // iconValue: listOfPowerHogs[4]
    41. // instrumentedStatus: listOfPowerHogs[5]
    42. // }
    43. // PowerCell {
    44. // width: (parent.width-100)/4
    45. // iconValue: listOfPowerHogs[6]
    46. // instrumentedStatus: listOfPowerHogs[7]
    47. // }
    48.  
    49. genList.createGrid(listOfPowerHogs)
    50.  
    51. }
    52. states: State {
    53. name: "ShowBars"
    54. when: view.movingVertically || view.movingHorizontally
    55.  
    56. }
    57. transitions: Transition {
    58. NumberAnimation { properties: "opacity"; duration: 400 }
    59. }
    60. }
    61. ScrollBar {
    62. id: horizontalScrollBar
    63. width: flick.width-12; height: 12
    64. anchors.bottom: flick.bottom
    65. opacity: 0
    66. orientation: Qt.Horizontal
    67. position: flick.visibleArea.xPosition
    68. pageSize: flick.visibleArea.widthRatio
    69. }
    70. }
    71. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to call a javascript function without the trigger of any event in a grid?

    Is loop.js in the same directory as the qml file? What is the contents of loop.js?

  3. #3
    Join Date
    Nov 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to call a javascript function without the trigger of any event in a grid?

    yes loop.js is in the same directory

    Loop.js has a fucntion which has the variant list as parameter.
    Here i am using power cells defined by me as a element on which the loop is being called on.
    Below is the code for loop.js
    Qt Code:
    1. var PCell = new Array;
    2. var i = 0;
    3. var powerComponent = Qt.createComponent("PowerCell.qml");
    4.  
    5. function setPowerCells(pList)
    6. {
    7. PCell = pList;
    8. for(i = 0; i < (PCell.length - 1);i+2)
    9. {
    10. PCell.iconValue = pList[i];
    11. PCell.instrumentedStatus = pList[i+1];
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 


    This is the code for power cell to be called by js file

    Qt Code:
    1. import Qt 4.7
    2.  
    3. Item {
    4. id: button
    5. signal clicked
    6. property string iconValue
    7. property string instrumentedStatus
    8. signal name
    9. property bool toggled: false
    10. width: 133
    11. height: 135
    12. Image {
    13. id:button_img
    14. anchors.fill: button
    15. anchors.margins: mouseArea.pressed
    16. smooth: true
    17. source: { if ("instrumented" == button.instrumentedStatus)
    18. return "instrumentedButton.png"
    19. else if ("non-instrumented" == button.instrumentedStatus)
    20. return "nonInstrumentedButton.png"
    21. }
    22.  
    23. Image {
    24. x:37
    25. y:40
    26. width: 54
    27. height: 56
    28. id: instrumentIcon
    29. source: button.iconValue
    30.  
    31. }
    32.  
    33. }
    34.  
    35. MouseArea {
    36. id: mouseArea
    37. anchors.fill: parent
    38. onClicked: {
    39. button.clicked()
    40. }
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 13:40
  2. Replies: 0
    Last Post: 20th December 2009, 16:37
  3. [noob] How can I have a widget event call a function?
    By Envergure in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2008, 00:17
  4. Replies: 3
    Last Post: 14th October 2008, 22:04
  5. Replies: 3
    Last Post: 13th September 2007, 11:29

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.