Results 1 to 9 of 9

Thread: How to assign model roles to delegates

  1. #1
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Question How to assign model roles to delegates

    Is there any way i can specify the role name accessible to the delegate of Repeaters and ListViews at run time? For instance i have the following GenericRepeater {} that I want to reuse. I have lot of QAbstractListModel derived classes in C++. Many of them have different rolenames and different number of roles (if they all had just one role I could have accessed it by mentioning modelData, but moment there are more than one, modelData is no longer provided by Repeaters/ListViews to their delegates).

    Here is the Repeater code:

    //GenericRepeater.qml
    Qt Code:
    1. Column {
    2. property alias m_refModel: objRepeater.model
    3.  
    4. Repeater {
    5. id: objRepeater
    6.  
    7. delegate: Rectangle {
    8. height: 40; width: 200
    9. color: "#000000"
    10. Text {
    11. color: "#ffffff"
    12. anchors.fill: parent
    13. text: model.genericRole //-----(1) What can I write here
    14. }
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    I want to use it in many places as:
    //SomeFile.qml
    Qt Code:
    1. GenericRepeater { m_refModel: myCppClass0 }
    2. GenericRepeater { m_refModel: myCppClass1 }
    To copy to clipboard, switch view to plain text mode 
    //SomeOtherFile.qml
    Qt Code:
    1. GenericRepeater { m_refModel: myCppClass2 }
    To copy to clipboard, switch view to plain text mode 

    where myCppClass<n> are objects of classes derived from QAbstractListModel and each of those classes can have any number of roles each arbitarily named. Ofcourse the code will fail at (1) above. How do I use GenericRepeater in SomeFile.qml etc ? Just like I assign various models to GenericRepeater {} can I assign the roles too ? If no, then is there an alternate way to realize this ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to assign model roles to delegates

    You can have a proxy model that does custom mapping of model roles.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to assign model roles to delegates

    Have a look at QAbstractItemModel::setRoleNames
    Oops,, Sorry I seem to have overlooked the problem in hurry. You can go with as Wysota said

  4. #4
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default Re: How to assign model roles to delegates

    Hi,
    I've found one solution to letting the user input the role at runtime but i don't know if it's a standard way and should be used.

    The roles are usually accessed as:
    Qt Code:
    1. text: model.someRole
    To copy to clipboard, switch view to plain text mode 
    But as stated this is like hard-coding the role. If instead I want the user to input the role via line-edit and show that instead, there is no way I can do it here
    but this seems to be equivalent:
    Qt Code:
    1. text: model["someRole"]
    To copy to clipboard, switch view to plain text mode 
    Bingo!
    Now that since it is a string, the user can easily input it during the runtime. But is this a standard way or it just happens to work?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to assign model roles to delegates

    I think that is just fine.

    As wysota said, a proxy model might even allow to keep the traditional syntax, by doing the mapping internally.

    Cheers,
    _

  6. #6
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default Re: How to assign model roles to delegates

    I think that is just fine.
    <1> So why isn't it documented? Seems very important to me - Hard-coding vs freedom, you choose.

    a proxy model might even allow to keep the traditional syntax, by doing the mapping internally
    I'm guilty of not being able to wrap my head around that

    <2> What exactly does that mean? My code is the file GenericRepeater.qml. People using my code will write files such as SomeFile.qml and SomeOtherFile.qml (refer to the 1st post in this thread). How should the structure of GenericRepeater.qml be?

    Here is an attempt at the public interface:

    Qt Code:
    1. Column {
    2. //--- begin public interface ---
    3. property var m_refModel //this is what the user will supply
    4. property string m_roleToUse //this is what the user will supply
    5. //--- end public interface ---
    6.  
    7. Repeater {
    8. id: objRepeater
    9.  
    10. model: //--------------(1) what should I give here?
    11.  
    12. delegate: Rectangle {
    13. height: 40; width: 200
    14. color: "#000000"
    15. Text {
    16. color: "#ffffff"
    17. anchors.fill: parent
    18. text: //--------------(2) What role should i give here?
    19. }
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    Questions and comments are inlined.

    Can you pls answer those?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to assign model roles to delegates

    Quote Originally Posted by ustulation View Post
    <1> So why isn't it documented?
    It is documented. It is part of Javascript specification where object.property is equivalent to object["property"]. Which also implies by the way that if you have a function "fun" in object "object" you can call it via object.fun() or object["fun"]().

    Ad 2.: m_refModel and modelData[m_roleToUse]
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    ustulation (13th February 2014)

  9. #8
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default Re: How to assign model roles to delegates

    It is documented. It is part of Javascript specification where object.property is equivalent to object["property"]. Which also implies by the way that if you have a function "fun" in object "object" you can call it via object.fun() or object["fun"]().
    ooo that was subtle for me. I's too busy thinking of QML syntax that i forgot it's javascript working. Thanks a lot for that !

    m_refModel and modelData[m_roleToUse]
    ya that's what i mentioned in #4. That is clear. What was not clear was your original proposal to use a proxying model somewhere in between (if i understood correctly) m_refModel and Repeater::model which would continue to allow me to use the
    tradational syntax
    as anda_skoa said.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to assign model roles to delegates

    You put a proxy model between a base model and a view. That proxy model can take care of mapping roles based on the base model data and its own role specification.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 9
    Last Post: 8th November 2016, 06:20
  2. QComboBox roles question
    By mentalmushroom in forum Qt Programming
    Replies: 9
    Last Post: 27th April 2012, 22:04
  3. Proxy Model and Item delegates causing issues
    By snydesc in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2012, 18:17
  4. Combine custom delegate and model data() roles
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2011, 12:43
  5. Getting all roles of specified column and/or model
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2010, 11:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.