ReferenceError: Component is not defined - QML Dynamic object creation
ClickableRectangle.qml
Code:
import QtQuick 2.0;
Rectangle
{
width: 40; height: 40; x: 0; y: 0;
// Fluorescent green
color:'#23FF23';
border.color: 'black'; border.width: 2; radius: 100
property int key;
signal iHaveBeenCalled (var key);
MouseArea
{
anchors.fill: parent;
onClicked:
{
iHaveBeenCalled.call (0, key)
}
}
}
GUIControllers.js
Code:
.import "GlobalVariablesAndFunctions.js" as GlobalVariablesAndFunctions
function createDynamicRectangles ()
{
/// Returns a Component object created using the QML file at the specified url, or null if an empty
/// string was given.
var component = Qt.createComponent ("ClickableRectangle.qml");
/// The returned component's Component::status property indicates whether the component was
/// successfully created.
if (component.status === Component.Ready)
{
/// arrayOfXAxixOfPoints and arrayOfYAxixOfPoints are the arrays holding x and y axis coordinates
/// of the rectangles (respectively) to be plotted.
/// Both these arrays are techincally supposed to have equal number of points which will the
/// determine number of rectangles to be plotted.
for (var i = 0; i < GlobalVariablesAndFunctions.arrayOfXAxixOfPoints.length; i++)
{
dynamicRectangles[i] = component.createObject (vehicleDrivingAreaRect, {x: 0, y: 0})
/// After an object of the component is created at a specified default position (0, 0) in our
/// case, we can calculate and reset its position and other properties at leisure as follows.
dynamicRectangles[i].x = GlobalVariablesAndFunctions.arrayOfXAxixOfPoints[i] - (dynamicRectangles[i].width / 2)
dynamicRectangles[i].y = GlobalVariablesAndFunctions.arrayOfYAxixOfPoints[i] - (dynamicRectangles[i].height / 2)
}
numberOfDynamicRectanglesActive = GlobalVariablesAndFunctions.arrayOfXAxixOfPoints.length
}
}
main.qml
Code:
import QtQuick 2.0
import "GUIControllers.js" as GUIControllers
Rectangle
{
id: rootContainer
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
GUIControllers.createDynamicRectangles()
}
}
}
The error in the title appears in GUIControllers.js at this line:
Code:
if (component.status === Component.Ready)
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.
Code:
.import QtQml 2.0 as QML
and then use QML.Component
Any specific reason you are not just using a Loader?
Cheers,
_
Re: ReferenceError: Component is not defined - QML Dynamic object creation
Quote:
Originally Posted by
anda_skoa
Any specific reason you are not just using a Loader?
And/or a Repeater...
Code:
Item {
id: vehicleDrivingAreaRect
Repeater {
model: GlobalVariablesAndFunctions.arrayOfXAxixOfPoints
ClickableRectangle {
x: modelData.x - width/2
y: modelData.y - height/2
}
}
}
Re: ReferenceError: Component is not defined - QML Dynamic object creation
Quote:
Originally Posted by
wysota
And/or a Repeater...
Right, I was actually thinking of Repeater, no idea why I wrote Loader :)
Cheers,
_
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.
Re: ReferenceError: Component is not defined - QML Dynamic object creation
Of course you can. It is even shown in code in my previous post.
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?
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 :)
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?
Re: ReferenceError: Component is not defined - QML Dynamic object creation
Quote:
Originally Posted by
TheIndependentAquarius
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.
Re: ReferenceError: Component is not defined - QML Dynamic object creation