Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: Component is not defined - QML Dynamic object creation

ClickableRectangle.qml

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

.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

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:
if (component.status === Component.Ready)

like image 459
Aquarius_Girl Avatar asked Jul 29 '14 10:07

Aquarius_Girl


People also ask

How to create a QML object from an existing QML component?

With component initialized you can call then Component.CreateObject. Or you could have used one parentComponent for that: parentComponent.CreateObject (QmlItem, "QML properties"). I usually use Qt.createQmlObject for doing dynamic things with QML window immediately in this QML file and don't create any new QML context for it.

When to use Qt createqmlobject?

I usually use Qt.createQmlObject for doing dynamic things with QML window immediately in this QML file and don't create any new QML context for it. Mind that in most cases you don't even need to use neither of the above but manipulate some QML Repeater and the data model for it.

What is the difference between createcomponent() and createqmlobject()?

If Qt.createComponent () is used, the creation context is the QQmlContext in which this method is called If Qt.createQmlObject () is called, the creation context is the context of the parent object passed to this method

How to create a component dynamically in Qt?

For the case like yours use Qt.createComponent to dynamically initialize the component you use. With component initialized you can call then Component.CreateObject. Or you could have used one parentComponent for that: parentComponent.CreateObject (QmlItem, "QML properties").


1 Answers

We need to import QtQuick 2.0 in GUIControllers.js as follows:

.import QtQuick 2.0 as QtQuickModuleImportedInJS

Then if we edit the culprit line as follows, the error vanishes:

if (component.status === QtQuickModuleImportedInJS.Component.Ready)

like image 123
Aquarius_Girl Avatar answered Oct 18 '22 09:10

Aquarius_Girl