Results 1 to 11 of 11

Thread: ReferenceError: Component is not defined - QML Dynamic object creation

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

    Default ReferenceError: Component is not defined - QML Dynamic object creation

    ClickableRectangle.qml

    Qt Code:
    1. import QtQuick 2.0;
    2.  
    3. Rectangle
    4. {
    5. width: 40; height: 40; x: 0; y: 0;
    6.  
    7. // Fluorescent green
    8. color:'#23FF23';
    9.  
    10. border.color: 'black'; border.width: 2; radius: 100
    11.  
    12. property int key;
    13. signal iHaveBeenCalled (var key);
    14.  
    15. MouseArea
    16. {
    17. anchors.fill: parent;
    18. onClicked:
    19. {
    20. iHaveBeenCalled.call (0, key)
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 



    GUIControllers.js


    Qt Code:
    1. .import "GlobalVariablesAndFunctions.js" as GlobalVariablesAndFunctions
    2.  
    3. function createDynamicRectangles ()
    4. {
    5. /// Returns a Component object created using the QML file at the specified url, or null if an empty
    6. /// string was given.
    7. var component = Qt.createComponent ("ClickableRectangle.qml");
    8. /// The returned component's Component::status property indicates whether the component was
    9. /// successfully created.
    10. if (component.status === Component.Ready)
    11. {
    12. /// arrayOfXAxixOfPoints and arrayOfYAxixOfPoints are the arrays holding x and y axis coordinates
    13. /// of the rectangles (respectively) to be plotted.
    14. /// Both these arrays are techincally supposed to have equal number of points which will the
    15. /// determine number of rectangles to be plotted.
    16. for (var i = 0; i < GlobalVariablesAndFunctions.arrayOfXAxixOfPoints.length; i++)
    17. {
    18. dynamicRectangles[i] = component.createObject (vehicleDrivingAreaRect, {x: 0, y: 0})
    19.  
    20. /// After an object of the component is created at a specified default position (0, 0) in our
    21. /// case, we can calculate and reset its position and other properties at leisure as follows.
    22. dynamicRectangles[i].x = GlobalVariablesAndFunctions.arrayOfXAxixOfPoints[i] - (dynamicRectangles[i].width / 2)
    23. dynamicRectangles[i].y = GlobalVariablesAndFunctions.arrayOfYAxixOfPoints[i] - (dynamicRectangles[i].height / 2)
    24. }
    25.  
    26. numberOfDynamicRectanglesActive = GlobalVariablesAndFunctions.arrayOfXAxixOfPoints.length
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    main.qml

    Qt Code:
    1. import QtQuick 2.0
    2. import "GUIControllers.js" as GUIControllers
    3.  
    4. Rectangle
    5. {
    6. id: rootContainer
    7. width: 360
    8. height: 360
    9. Text {
    10. text: qsTr("Hello World")
    11. anchors.centerIn: parent
    12. }
    13. MouseArea {
    14. anchors.fill: parent
    15. onClicked: {
    16. GUIControllers.createDynamicRectangles()
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    The error in the title appears in GUIControllers.js at this line:
    Qt Code:
    1. if (component.status === Component.Ready)
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: ReferenceError: Component is not defined - QML Dynamic object creation

    Your script is missing the import for QtQml 2.0, which contains the Component type (or QtQuick 2.0 if you prefer)
    E.g.
    Qt Code:
    1. .import QtQml 2.0 as QML
    To copy to clipboard, switch view to plain text mode 
    and then use QML.Component

    Any specific reason you are not just using a Loader?

    Cheers,
    _

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

    TheIndependentAquarius (30th July 2014)

  4. #3
    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: ReferenceError: Component is not defined - QML Dynamic object creation

    Quote Originally Posted by anda_skoa View Post
    Any specific reason you are not just using a Loader?
    And/or a Repeater...

    javascript Code:
    1. Item {
    2. id: vehicleDrivingAreaRect
    3.  
    4. Repeater {
    5. model: GlobalVariablesAndFunctions.arrayOfXAxixOfPoints
    6.  
    7. ClickableRectangle {
    8. x: modelData.x - width/2
    9. y: modelData.y - height/2
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    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.


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

    TheIndependentAquarius (30th July 2014)

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

    Default Re: ReferenceError: Component is not defined - QML Dynamic object creation

    Quote Originally Posted by wysota View Post
    And/or a Repeater...
    Right, I was actually thinking of Repeater, no idea why I wrote Loader

    Cheers,
    _

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

    Default Re: ReferenceError: Component is not defined - QML Dynamic object creation

    Thankful to both of you.

    I don't know if we can set the internal properties like x,y, color, etc "different" for each element if we use a repeater. Will study on that. Thanks.

  8. #6
    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: ReferenceError: Component is not defined - QML Dynamic object creation

    Of course you can. It is even shown in code in my previous post.
    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.


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

    TheIndependentAquarius (30th July 2014)

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

    Default Re: ReferenceError: Component is not defined - QML Dynamic object creation

    Thanks for reminding.

    BTW, do I have anything to lose if I create component dynamically rather than using repeaters?

  11. #8
    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: ReferenceError: Component is not defined - QML Dynamic object creation

    Repeater also creates them dynamically. And you can lose maintanance of an ugly javascript file
    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.


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

    Default Re: ReferenceError: Component is not defined - QML Dynamic object creation

    Well, then can you list some cases where using Qt.createComponent is "necessary" and repeater can simply not do the job?

  13. #10
    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: ReferenceError: Component is not defined - QML Dynamic object creation

    Quote Originally Posted by TheIndependentAquarius View Post
    Well, then can you list some cases where using Qt.createComponent is "necessary" and repeater can simply not do the job?
    E.g. when the life-time of each created object is independent from the life-time of other objects.
    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.


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

    TheIndependentAquarius (1st August 2014)

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

    Default Re: ReferenceError: Component is not defined - QML Dynamic object creation

    Thanks for the help.

Similar Threads

  1. QMetaType dynamic object creation and initialization
    By d_stranz in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2014, 11:12
  2. Dynamic creation of widget with N images
    By davethomaspilot in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2014, 13:45
  3. Replies: 1
    Last Post: 8th November 2011, 23:27
  4. Cancelling an Object Creation
    By bbad68 in forum Newbie
    Replies: 1
    Last Post: 4th March 2010, 21:22
  5. Dynamic widget creation from an XML-like file
    By ttvo in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 23:15

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.