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. Distinguish between the models 'id' and the local/view 'id'.
Forum Updated to NodeBB v4.3 + New Features

Distinguish between the models 'id' and the local/view 'id'.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 400 Views 2 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.
  • J Offline
    J Offline
    javs
    wrote on last edited by
    #1

    Hi all,

    I am trying to use the id field of my model in the views delegate.
    I can't seem to find how to make the distinguishment between the models 'id' and the local 'id'.
    How would i go about that?
    See below.

    QHash<int, QByteArray> MyModel::roleNames() const
    {
    	static const QHash<int, QByteArray> roles {
    		{NameRole, "name"},
    		{IdRole, "id"},
    		{ChildCountRole, "childCount"},
    		{Active, "active"},
    	};
    	return roles;
    }
    
    ItemDelegate {
        id: delegate
    
        contentItem: ListButton {
            id: id // <= I want the buttons id to be the models id ...
            caption: name // <= ... just like how the models name field is assigned to the caption of the button.
        }
    }
    
    B 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      That doesn't really make sense.

      The QML id is not a property and is not modifiable at runtime.
      It is fixed at compile time and is used as the identifier in the current context. A single object can have multiple id in different contexts..

      What are you trying to do? Why do you want a dynamic id?

      If the question were for a different role, says you had a caption role, you could differentiate them by prefixing the role with model. :

      caption: model.caption
      
      1 Reply Last reply
      0
      • J javs

        Hi all,

        I am trying to use the id field of my model in the views delegate.
        I can't seem to find how to make the distinguishment between the models 'id' and the local 'id'.
        How would i go about that?
        See below.

        QHash<int, QByteArray> MyModel::roleNames() const
        {
        	static const QHash<int, QByteArray> roles {
        		{NameRole, "name"},
        		{IdRole, "id"},
        		{ChildCountRole, "childCount"},
        		{Active, "active"},
        	};
        	return roles;
        }
        
        ItemDelegate {
            id: delegate
        
            contentItem: ListButton {
                id: id // <= I want the buttons id to be the models id ...
                caption: name // <= ... just like how the models name field is assigned to the caption of the button.
            }
        }
        
        B Offline
        B Offline
        Bob64
        wrote on last edited by
        #3

        @javs I don't know if I am missing something, but couldn't you just give it a different name?

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #4

          I have run into serious problems when I shadow names of features or properties in Item or derived from Item. It is better to just name it something that cannot clash.

          static const QHash<int, QByteArray> roles {
          		{NameRole, "nameRole"},
          		{IdRole, "idRole"},
          		{ChildCountRole, "childCountRole"},
          		{ActiveRole, "activeRole"},
          	};
          

          Putting role in the name automatically documents this is coming from the model.

          Using simple names that have a chance to clash just opens you up to failing later because something is added to an object later. Analogous to using i, j, and k in every loop. It is a pain to diagnose. It is a bad habit that will cost you time months from now. Text is cheap, descriptive names don't cost you anything, but can cost you hours if you collide with other names. Search and replace is a thing. Autocomplete is a thing. Use them to supplement having to type in long variable names.

          C++ is a perfectly valid school of magic.

          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