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. How to Assing an id property for dynamically created element.

How to Assing an id property for dynamically created element.

Scheduled Pinned Locked Moved QML and Qt Quick
20 Posts 6 Posters 17.8k 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.
  • S Offline
    S Offline
    syrianzoro
    wrote on 13 Dec 2010, 11:00 last edited by
    #1

    This is Javascript code
    @

    function createComboBox(row, column)
    {
    if (component == null)
    component = Qt.createComponent("CompoBox.qml");

    if (component.status == Component.Ready) {
        var dynamicObject = component.createObject(cell);
        if (dynamicObject == null) {
            console.log("error creating Cell");
            console.log(component.errorString());
            return false;
        }
    
        dynamicObject.width = gridColumn.cellWidth;
        dynamicObject.height = gridColumn.cellHeight;
        dynamicObject.anchors.id = ?????
    
      
    } else {
        console.log("error creating Comobobox");
        console.log(component.errorString());
        return false;
    }
    return true;
    

    }@

    Qt is the future

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stukdev
      wrote on 13 Dec 2010, 11:08 last edited by
      #2

      I dont understand what you want do.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kamalakshantv
        wrote on 13 Dec 2010, 11:44 last edited by
        #3

        AFAIK you cannot set id of a QML element from c++

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on 14 Dec 2010, 06:29 last edited by
          #4

          Hi,

          It isn't possible to add an id to a dynamically created object (the set of ids in a component is considered static). Can you say any more about what you want to use the id for? There might be some alternative methods/workarounds for achieving what you are after.

          Regards,
          Michael

          1 Reply Last reply
          0
          • S Offline
            S Offline
            syrianzoro
            wrote on 14 Dec 2010, 12:48 last edited by
            #5

            thanks stuk, QtK and mbrasser

            I want to create a grid dynamically , the dataset is SQL table I don't know the the fields and the datatypes which the user may select using SELECT statment.

            Qt is the future

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kxyu
              wrote on 14 Dec 2010, 14:28 last edited by
              #6

              you don't need an id for dynamic object, just use object name to operate

              1 Reply Last reply
              0
              • S Offline
                S Offline
                syrianzoro
                wrote on 14 Dec 2010, 19:23 last edited by
                #7

                suppose I use another javascript function to create more than one object using the previous function??
                the id or name has to be unique ,I think it should be created dynamically

                Qt is the future

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mbrasser
                  wrote on 15 Dec 2010, 00:17 last edited by
                  #8

                  It might be helpful to look at the samegame demo, which manages a large number of dynamically created objects (all of the balls are dynamically created). If you are presenting data from a dataset, I'd also suggest seeing if you can accomplish what you need with the model/view (or model with Repeater) architecture, as it takes care of the object management tasks for you.

                  Regards,
                  Michael

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    syrianzoro
                    wrote on 15 Dec 2010, 08:48 last edited by
                    #9

                    thank you mbrasser

                    I tired all the ways you had suggest,in the samegame they redraw the objects again but that will be slow and boring, if we can access the object directly ,we will decrease the code and cost.

                    Qt is the future

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      xsacha
                      wrote on 15 Dec 2010, 09:06 last edited by
                      #10

                      If you use a ListModel/Delegate method, of course, you do not have such a problem as you can refer to each item by an index. You can also get item at mouse coords (or x,y coords).

                      You can use a ListModel/Delegate to dynamically create items inside a GridView in exactly the way you seem to be doing.

                      Also, if you use any sort of List/indexed element (Flow), you will have the same functionality.

                      So basically, the only area this falls down is if you try to use id's from non-QML code.

                      Solution: Use QML.

                      • Sacha
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        syrianzoro
                        wrote on 15 Dec 2010, 11:28 last edited by
                        #11

                        Mr xsacha
                        You have no way to dynamically make a grid using ListModel/Delegate with one dataset!! so you have to draw the grid and populate it with suitable data.

                        the cell has to make sense to the user actions regardless of the GridComponent so the need to Know the id is very required

                        Qt is the future

                        1 Reply Last reply
                        0
                        • X Offline
                          X Offline
                          xsacha
                          wrote on 15 Dec 2010, 11:35 last edited by
                          #12

                          You can add elements to a model at any time (dynamically).
                          Each element can be uniquely identified with a property (eg. name) or an index.

                          I don't understand what you mean but I assume you think the model is limited to static ListElements?

                          You can use insert (or append):
                          @fruitModel.insert(2, {"cost": 5.95, "name":"Pizza", "shape":"Rectangle"})@

                          You can dynamically change properties:
                          @fruitModel.setProperty(index, "cost", cost * 2)@

                          You can then refer to these elements:
                          @index@
                          @grid.indexAt(mouseX, mouseY)@
                          @fruitModel.get(index).name@

                          • Sacha
                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            syrianzoro
                            wrote on 15 Dec 2010, 11:38 last edited by
                            #13

                            but you have no way to make a grid using one model??

                            Qt is the future

                            1 Reply Last reply
                            0
                            • X Offline
                              X Offline
                              xsacha
                              wrote on 15 Dec 2010, 11:44 last edited by
                              #14

                              @
                              Component {
                              id: widgetDelegate
                              Rectangle {
                              width: 80; height: 80
                              radius: shape == "Circle" ? 80 : 0
                              }
                              }
                              GridView {
                              model: WidgetModel { id: widgetModel }
                              delegate: widgetDelegate
                              }@

                              This is a GridView with one model?

                              The dataset can come from an online feed, from XML (like Flickr/Facebook/Twitter or a personal database), or a simple ListModel type.

                              Did you mean multiple models?

                              • Sacha
                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                syrianzoro
                                wrote on 15 Dec 2010, 12:03 last edited by
                                #15

                                suppose you has a database table Persons(id,name,phone,address,JobTitle,etc) . Gridview has one model , and one model can display predefined data row , suppose I made the following query (SELECT ID,ADDRESS FROM PERSONS) ???? and in another time the query (SELECT ETC FROM PERSONS)??

                                Qt is the future

                                1 Reply Last reply
                                0
                                • X Offline
                                  X Offline
                                  xsacha
                                  wrote on 15 Dec 2010, 12:16 last edited by
                                  #16

                                  Yes, I have done this a few times.

                                  Have a look at the Twitter app in the SDK that uses very similar 'SELECT' style commands via an XML Model.

                                  Alternatively, you can use a ListModel and sort it yourself.
                                  Remember, you can change the model at any time and you can have temporary models that hold all the information but are never used if you like.

                                  • Sacha
                                  1 Reply Last reply
                                  0
                                  • X Offline
                                    X Offline
                                    xsacha
                                    wrote on 15 Dec 2010, 12:24 last edited by
                                    #17

                                    Here's an example to demonstrate: SELECT "flagged" FROM name IN widgetmodel (pseudo sql query)
                                    Using a ListModel:

                                    @GridView {
                                    model: WidgetModel { id: widgetModel }
                                    delegate: widgetDelegate
                                    MouseArea {
                                    id: selectFlaggedFromName
                                    anchors.fill: parent
                                    onClicked: {
                                    for (var i = 0; i < widgetModel.count; i++)
                                    {
                                    if (widgetModel.get(i).name != "flagged")
                                    widgetModel.remove(i--)
                                    }
                                    }
                                    }
                                    }@

                                    Working fine here.

                                    • Sacha
                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      syrianzoro
                                      wrote on 15 Dec 2010, 12:50 last edited by
                                      #18

                                      thank you xsacha I will review that and of course feed you back about?

                                      Qt is the future

                                      1 Reply Last reply
                                      0
                                      • X Offline
                                        X Offline
                                        xsacha
                                        wrote on 15 Dec 2010, 14:56 last edited by
                                        #19

                                        This has been made in to a Wiki Entry: http://developer.qt.nokia.com/wiki/Real-time_Sorting_and_Filtering_of_a_GridView

                                        Enjoy!

                                        • Sacha
                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          syrianzoro
                                          wrote on 3 Jan 2011, 09:57 last edited by
                                          #20

                                          assistant say:
                                          *QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML can only display list data. *

                                          Qt is the future

                                          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