Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Object reference question
QtWS25 Last Chance

QML Object reference question

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 686 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    heatblazer
    wrote on last edited by
    #1

    Hello, I am quite new in QT and QML and Id like to ask about object life in QML. I have a sample code for a "small game". The idea is to press the same colors with both fingers ( touch only) and they will vanish. Its in concept only, and I`ve made some small code to aid it. The idea is that the screen is populated with random images ( colors with Rect in my case ). When I click on specific Rect - it vanishes. Here is my main.qml file ( where all starts ).
    @
    import QtQuick 2.0
    import "Engine.js" as Engine
    import "ObjectCreator.js" as ObjectCreator

    Item {
    id: main
    Component.onCompleted: (function() {
    Engine.Engine.createScreen(0,0, 640, 480);
    (function() {
    var color = ["red", "green", "blue", "black", "pink","yellow"];
    var tmp = 0;
    for ( var i=0; i <640 ; i+= 40) {
    for ( var j=0; j < 480; j+= 40) {
    (function(arg){
    console.log(Math.floor(Math.random()*color.length));
    Engine.Engine.createObject(
    i, j, 40, 40,
    color[Math.floor(Math.random()*color.length)]
    , "o"+tmp,
    function() {
    Engine.Engine.getObjectByIndex(arg).getSelf().destroy();
    });
    })(tmp++);
    } //emd for() 2
    } // end for() 1
    })();
    })();
    MouseArea {
    /*optional */
    id: event
    anchors.fill: parent
    onClicked: function () {}

    }
    Timer {
        id: mainThread;
        interval: 100;
        running: true;
        repeat: true;
        onTriggered: function(){
            var o = Engine.Engine.getAllObjects();
             for(var i=0; i < o.length; i++) {
    
             }
        };
    }
    

    } //end main
    @
    When I run it - on click it performs as expected - the object is destroyed and a white field replaces it, however, the log says : "file:///home/ilian/QT5/untitled2/inline:1: TypeError: Cannot read property of null". I want to ask - is it safe or is it malicious? There is no object in the clicked area so can I just skip the error or I must make some checks to avoid it?
    I cant add Engine.js, CallStack.js and ObjectCreator.js since they are large, but the idea is that Engine calls ObjectCreator which makes a Rect in X,Y with Width and Height and color, and adds a function to Objects private CallStack for further usage. For now I just add a foo and pop it from 0 index. In my case the method passed in the ObjectCreation so click performs destroy(). getSelf() return reference to the Rect object - not to the ObjectCreator, Engine has a list of ObjectCreators - the class is not factory and the name should be Object for convinience but for now Ive left it as it is, since Ive planned it to be a factory.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved