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. [solved] How can I make something like Qt.createComponent(Rectangle) ?
Forum Updated to NodeBB v4.3 + New Features

[solved] How can I make something like Qt.createComponent(Rectangle) ?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 6.0k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    icetbr
    wrote on last edited by
    #1

    I want a Rectangle created from javascript. I'm not proficient with Qt. I can't do that in QML right? Only if I use @createQmlObject@.

    Also, how can I access the Qt Quick Components that I have in my QTDIR\imports folder? Something like

    @Qt.createComponent(Qt.SystemVariable("QTDIR") + "\imports\myPlugin\MyComponent.qml")@

    Thanks

    1 Reply Last reply
    0
    • B Offline
      B Offline
      baysmith
      wrote on last edited by
      #2

      You can create QML objects from JavaScript. For example, this create 10 Rectangle elements positioned manually on a diagonal.

      @import QtQuick 1.0

      Item {
      width: 640
      height: 480

      Item {
          function createItems() {
              var itemList = []
              for (var i = 0; i < 10; ++i) {
                  itemList[i] = Qt.createQmlObject(
                              'import QtQuick 1.0; Rectangle { width: 10; height: 10;'
                              + ' x: ' + i*20 + '; y: ' + i*20 + '; color: "blue" }',
                              parent, "createItems()");
              }
              return itemList
          }
          property variant items: createItems()
      }
      

      }@

      To access a plugin use, assuming the plugin is properly setup.

      @import myPlugin 1.0@

      Nokia Certified Qt Specialist.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        syrianzoro
        wrote on last edited by
        #3

        Copy the following code and put them together in the same dir

        MyJava.js
        @var component;

        function createMyRect(vWidth,vHeight) {
        if (component == null)
        component = Qt.createComponent("MyRect.qml");

        if (component.status == Component.Ready) {
            var dynamicObject = component.createObject(myrect);
            if (dynamicObject == null) {
                console.log("error creating Myrect");
                console.log(component.errorString());
                return false;
            }
        
            dynamicObject.width= vWidth;
            dynamicObject.height = vHeight;
            dynamicObject.color = "red";
        
        } else {
            console.log("error loading block component");
            console.log(component.errorString());
            return false;
        }
        return true;
        

        }
        @

        the second : MyRect.qml

        @import Qt 4.7
        import "MyJava.js" as JavaCode

        Rectangle{
        id:myrect
        color:"lightgreen"

        width:200; height:200;
        
        MouseArea{
            anchors.fill:parent;
            onClicked:JavaCode.createMyRect(100,100);
        }
        

        }
        @

        Qt is the future

        1 Reply Last reply
        0
        • I Offline
          I Offline
          icetbr
          wrote on last edited by
          #4

          Alright, thanks for the ideas

          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