Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Arrays and structures in QML
Forum Updated to NodeBB v4.3 + New Features

Arrays and structures in QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
arraystructurearray of struct
15 Posts 3 Posters 31.7k 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.
  • A Alper
    18 May 2016, 12:37

    Hi
    Is it possible to define an array in qml?
    What about structure and array of structures?

    P Offline
    P Offline
    p3c0
    Moderators
    wrote on 18 May 2016, 16:54 last edited by
    #3

    @Alper QML has in-built support for Javascript. And then with Javascript objects you can simulate c like structures or array of structures.
    For eg:

    // A Js struct
    var person = { 
           firstName : "John", 
           age : 50, 
           male : true
    };
    
    // Array of struct
    var array = [
          { firstName : "John", age: 50, male: true },
          { firstName : "Jenny", age: 25, male: false },
    ];
    

    157

    A 1 Reply Last reply 18 May 2016, 18:19
    3
    • P p3c0
      18 May 2016, 16:54

      @Alper QML has in-built support for Javascript. And then with Javascript objects you can simulate c like structures or array of structures.
      For eg:

      // A Js struct
      var person = { 
             firstName : "John", 
             age : 50, 
             male : true
      };
      
      // Array of struct
      var array = [
            { firstName : "John", age: 50, male: true },
            { firstName : "Jenny", age: 25, male: false },
      ];
      
      A Offline
      A Offline
      Alper
      wrote on 18 May 2016, 18:19 last edited by
      #4

      @p3c0 Thank you
      sorry for my bad English
      I am working on a project that needs an application like windows explorer (win8 large icons style), the application window contains small windows inside(like file icons in windows explorer), i used Grid and scrollView. Icons,/small windows /components in main window can dragged by mouse, its working but my problem is on positioning the components, it can be dragged on another component and cover it (it must not be covered.)

      I think that i can use an array of structures and using of dragging event to positioning the components and sorting, grouping, arranging etc.
      Also the components are dynamic. (separate qml files load when need)
      Is there another/better way to do that?

      Thanks in advance

      P 1 Reply Last reply 19 May 2016, 05:10
      0
      • A Alper
        18 May 2016, 18:19

        @p3c0 Thank you
        sorry for my bad English
        I am working on a project that needs an application like windows explorer (win8 large icons style), the application window contains small windows inside(like file icons in windows explorer), i used Grid and scrollView. Icons,/small windows /components in main window can dragged by mouse, its working but my problem is on positioning the components, it can be dragged on another component and cover it (it must not be covered.)

        I think that i can use an array of structures and using of dragging event to positioning the components and sorting, grouping, arranging etc.
        Also the components are dynamic. (separate qml files load when need)
        Is there another/better way to do that?

        Thanks in advance

        P Offline
        P Offline
        p3c0
        Moderators
        wrote on 19 May 2016, 05:10 last edited by
        #5

        @Alper Ok. But what do you intend to store in array of structures ?

        157

        A 1 Reply Last reply 19 May 2016, 05:37
        1
        • P p3c0
          19 May 2016, 05:10

          @Alper Ok. But what do you intend to store in array of structures ?

          A Offline
          A Offline
          Alper
          wrote on 19 May 2016, 05:37 last edited by Alper
          #6

          @p3c0
          The width of container rectangle divides into width of component (number of columns : C) and height of container divides into height of container(number of rows: R).
          So i get a table width C columns and R rows, each Row covers y coordinate from y1 to y2 and each Column from x1 to x2.
          When a component dragged, its x,y coordinate will be checked to position in suitable place.

          for (i = 0 ; i<elementsOfTable;i++)
          if(component.x in array[i].x and component.y in array[i].y)
          {
          setComponentPosition in position i (position i x,y coordinates is in table)
          }

          i don't want do that in C++.
          Another thing i need is a scroll bar in thish manner. i don't know how to add it in this manner. because the Flickable and Grid will be removed.

          P 1 Reply Last reply 19 May 2016, 05:52
          0
          • A Alper
            19 May 2016, 05:37

            @p3c0
            The width of container rectangle divides into width of component (number of columns : C) and height of container divides into height of container(number of rows: R).
            So i get a table width C columns and R rows, each Row covers y coordinate from y1 to y2 and each Column from x1 to x2.
            When a component dragged, its x,y coordinate will be checked to position in suitable place.

            for (i = 0 ; i<elementsOfTable;i++)
            if(component.x in array[i].x and component.y in array[i].y)
            {
            setComponentPosition in position i (position i x,y coordinates is in table)
            }

            i don't want do that in C++.
            Another thing i need is a scroll bar in thish manner. i don't know how to add it in this manner. because the Flickable and Grid will be removed.

            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 19 May 2016, 05:52 last edited by
            #7

            @Alper Perhaps an array of point's ?

            Another thing i need is a scroll bar in thish manner. i don't know how to add it in this manner. because the Flickable and Grid will be removed.

            What type is the container then ?

            157

            A 1 Reply Last reply 19 May 2016, 05:59
            0
            • P p3c0
              19 May 2016, 05:52

              @Alper Perhaps an array of point's ?

              Another thing i need is a scroll bar in thish manner. i don't know how to add it in this manner. because the Flickable and Grid will be removed.

              What type is the container then ?

              A Offline
              A Offline
              Alper
              wrote on 19 May 2016, 05:59 last edited by Alper
              #8

              @p3c0 rectange

              an array of ranges of points.

              P 2 Replies Last reply 19 May 2016, 06:01
              0
              • A Alper
                19 May 2016, 05:59

                @p3c0 rectange

                an array of ranges of points.

                P Offline
                P Offline
                p3c0
                Moderators
                wrote on 19 May 2016, 06:01 last edited by
                #9

                @Alper Well how do you intend to scroll a rectangle ? Why not Flickable then ?

                157

                A 1 Reply Last reply 19 May 2016, 06:10
                0
                • A Alper
                  19 May 2016, 05:59

                  @p3c0 rectange

                  an array of ranges of points.

                  P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 19 May 2016, 06:09 last edited by
                  #10

                  @Alper Array of points

                  var arr = [
                              Qt.point(0,30),
                              Qt.point(40,32),
                              Qt.point(4,3),
                              Qt.point(5,6)
                          ];
                  

                  157

                  1 Reply Last reply
                  1
                  • P p3c0
                    19 May 2016, 06:01

                    @Alper Well how do you intend to scroll a rectangle ? Why not Flickable then ?

                    A Offline
                    A Offline
                    Alper
                    wrote on 19 May 2016, 06:10 last edited by Alper
                    #11

                    @p3c0 said:

                    intend

                    :) i intend to use every thing that does it!
                    But i don't know HOW!
                    I tried it but not worked:

                        function createComponent(){ //the app calls this function several times when running
                            {
                                yCoord += 100;    //i used it to check scrolling
                                var component = Qt.createComponent("qrc:/MyQML.qml");
                                var obj  = component.createObject(flickableContainer, {"x": 0, "y": yCoord });
                            }
                        }
                    
                    
                    
                        Flickable {
                            id:flickableContainer
                            anchors.fill: parent // parent is a rectangle
                            anchors.margins: 5
                        }
                        ScrollView {
                            contentItem :flickableContainer
                            width: parent.width
                            height: parent.height
                            x:flickableContainer.x
                            y:flickableContainer.y
                        }
                    
                    P 1 Reply Last reply 19 May 2016, 06:30
                    0
                    • A Alper
                      19 May 2016, 06:10

                      @p3c0 said:

                      intend

                      :) i intend to use every thing that does it!
                      But i don't know HOW!
                      I tried it but not worked:

                          function createComponent(){ //the app calls this function several times when running
                              {
                                  yCoord += 100;    //i used it to check scrolling
                                  var component = Qt.createComponent("qrc:/MyQML.qml");
                                  var obj  = component.createObject(flickableContainer, {"x": 0, "y": yCoord });
                              }
                          }
                      
                      
                      
                          Flickable {
                              id:flickableContainer
                              anchors.fill: parent // parent is a rectangle
                              anchors.margins: 5
                          }
                          ScrollView {
                              contentItem :flickableContainer
                              width: parent.width
                              height: parent.height
                              x:flickableContainer.x
                              y:flickableContainer.y
                          }
                      
                      P Offline
                      P Offline
                      p3c0
                      Moderators
                      wrote on 19 May 2016, 06:30 last edited by
                      #12

                      @Alper
                      Flickable should be inside ScrollView.
                      For eg:

                      ScrollView {
                          width: 200; height: 200
                          Flickable {
                              anchors.fill: parent
                              contentWidth: image.width; contentHeight: image.height
                              Image { id: image; source: "http://placehold.it/650x650&text=\"Qt\"" }
                          }
                      }
                      

                      And set ScrollView as a parent to dynamic component.
                      Btw. You can completely replace Flickable with ScrollView.

                      157

                      A 1 Reply Last reply 19 May 2016, 07:07
                      1
                      • P p3c0
                        19 May 2016, 06:30

                        @Alper
                        Flickable should be inside ScrollView.
                        For eg:

                        ScrollView {
                            width: 200; height: 200
                            Flickable {
                                anchors.fill: parent
                                contentWidth: image.width; contentHeight: image.height
                                Image { id: image; source: "http://placehold.it/650x650&text=\"Qt\"" }
                            }
                        }
                        

                        And set ScrollView as a parent to dynamic component.
                        Btw. You can completely replace Flickable with ScrollView.

                        A Offline
                        A Offline
                        Alper
                        wrote on 19 May 2016, 07:07 last edited by
                        #13

                        @p3c0 Thanks for your reply
                        It works for image as static object/element, how to use it for dynamic component?

                        P 2 Replies Last reply 19 May 2016, 08:04
                        0
                        • A Alper
                          19 May 2016, 07:07

                          @p3c0 Thanks for your reply
                          It works for image as static object/element, how to use it for dynamic component?

                          P Offline
                          P Offline
                          p3c0
                          Moderators
                          wrote on 19 May 2016, 08:04 last edited by
                          #14

                          @Alper It seems the scrollbars are not enabled when a component is added to it dynamically. Might be a bug or we are missing something? Will find it out.

                          157

                          1 Reply Last reply
                          1
                          • A Alper
                            19 May 2016, 07:07

                            @p3c0 Thanks for your reply
                            It works for image as static object/element, how to use it for dynamic component?

                            P Offline
                            P Offline
                            p3c0
                            Moderators
                            wrote on 19 May 2016, 09:11 last edited by
                            #15

                            Hi @Alper,
                            Here is what works.
                            Use ScrollView instead of Flickable for scrollbars. Then as per this we need to explicitly set the contentItem as this newly created component.
                            A very minimal example:

                            import QtQuick 2.6
                            import QtQuick.Controls 1.4
                            
                            Item {
                                id: root
                                width: 200
                                height: 200
                            
                                ScrollView {
                                    id: scroll
                                    anchors.fill: parent
                                }
                            
                                Button {
                                    text: "Load"
                                    onClicked: {
                                        var component = Qt.createComponent("Dummy.qml");
                                        var obj  = component.createObject(scroll);
                                        scroll.contentItem = obj
                                    }
                                }
                            }
                            
                            //Dummy.qml
                            
                            import QtQuick 2.6
                            
                            Image {
                                id: image;
                                source: "http://placehold.it/650x650&text=\"Qt\""
                            }
                            

                            157

                            1 Reply Last reply
                            0

                            12/15

                            19 May 2016, 06:30

                            • Login

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