Results 1 to 7 of 7

Thread: Problem with role assignment inside delegate component

  1. #1
    Join Date
    Mar 2013
    Posts
    18
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Problem with role assignment inside delegate component

    Hi all,

    I am new to QML and I am currently working at a reusable QML component.
    It is a ListView with a model class attached.
    The model is derived from QStandardItemModel with custom roles defined.
    Furthermore, the ListView uses a delegate component to render the data attached to the role.
    As I have got several model sub classes I would like to reuse my ListView with different models which in the end all define different roles.
    So far, everything works fine except re-use.

    In order to reuse my ListView I think I have to assign roles more dynamically e.g. configure the role the delegate should use for rendering from outside of my ListView component.

    Here is a short example of what I would like to achieve:

    main.qml:

    Qt Code:
    1. Rectangle
    2. {
    3. MyListView
    4. {
    5. id: taskList
    6.  
    7. model: taskModel //QStandardItemModel
    8. role: CustomDisplayText//<-role assignment does not work this way
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 


    MyListView.qml:
    Qt Code:
    1. MyListView
    2. {
    3. id: myListView
    4. objectName: "myListView"
    5. property variant role
    6.  
    7.  
    8. delegate: Rectangle
    9. {
    10. id: itemDelegate
    11. objectName: "itemDelegate"
    12.  
    13. color: itemDelegate.focus? highlightColor : backgroundColor
    14.  
    15. Text
    16. {
    17. id: itemTextField
    18. objectName: "itemTextField"
    19.  
    20. anchors.verticalCenter: itemDelegate.verticalCenter
    21.  
    22. text: role
    23. }
    24.  
    25. MouseArea
    26. {
    27. id: itemDelegateMouseArea
    28. objectName: "itemDelegateMouseArea"
    29. anchors.fill: parent
    30. onClicked:
    31. {
    32. itemDelegate.forceActiveFocus();
    33. itemSelected(model.index)
    34. }
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    In fact, the role assignment does not work this way.

    Can anyone help me solving my problem?

    Thanks in advance!

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

    Default Re: Problem with role assignment inside delegate component

    Pass the role by string and have a method in your model to retrieve data by role name.
    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
    Mar 2013
    Posts
    18
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Problem with role assignment inside delegate component

    Dear wysota,

    Thanks for your advice.

    I have already tried to pass the role by string this way:


    Qt Code:
    1. //main.qml
    2. Rectangle
    3. {
    4.  
    5. MyListView
    6.  
    7. {
    8.  
    9. id: taskList
    10.  
    11.  
    12.  
    13. model: taskModel //QStandardItemModel
    14.  
    15. role: "CustomDisplayText"//<-does not work either
    16.  
    17. }
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Furthermore, the models support the QStandardItemModel::data method with the role CustomDisplayText.

    If I pass the role name by string the delegate in MyListView shows "CustomDisplayText" instead of the data assigned to the role CustomDisplayText.

    Following works but MyListView is not reusable with my other models:


    Qt Code:
    1. //MyListView.qml
    2. MyListView
    3. {
    4.  
    5. id: myListView
    6.  
    7. objectName: "myListView"
    8.  
    9. property variant role
    10.  
    11.  
    12.  
    13.  
    14.  
    15. delegate: Rectangle
    16.  
    17. {
    18.  
    19. id: itemDelegate
    20.  
    21. objectName: "itemDelegate"
    22.  
    23.  
    24.  
    25. color: itemDelegate.focus? highlightColor : backgroundColor
    26.  
    27.  
    28.  
    29. Text
    30.  
    31. {
    32.  
    33. id: itemTextField
    34.  
    35. objectName: "itemTextField"
    36.  
    37.  
    38.  
    39. anchors.verticalCenter: itemDelegate.verticalCenter
    40.  
    41.  
    42.  
    43. text: CustomDisplayText//<-works but I cannot reuse MyListView with other role names
    44.  
    45. }
    46.  
    47.  
    48.  
    49. MouseArea
    50.  
    51. {
    52.  
    53. id: itemDelegateMouseArea
    54.  
    55. objectName: "itemDelegateMouseArea"
    56.  
    57. anchors.fill: parent
    58.  
    59. onClicked:
    60.  
    61. {
    62.  
    63. itemDelegate.forceActiveFocus();
    64.  
    65. itemSelected(model.index)
    66.  
    67. }
    68.  
    69. }
    70.  
    71. }
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 

    Am I doing something wrong?

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

    Default Re: Problem with role assignment inside delegate component

    Yes, you are not using a custom method that returns data using a role passed by string.
    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.


  5. #5
    Join Date
    Mar 2013
    Posts
    18
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Problem with role assignment inside delegate component

    Ok, I see. I will give it a try.

    The delegate has to use

    Qt Code:
    1. text: model.customMethod(Role)
    To copy to clipboard, switch view to plain text mode 

    to retrieve the data?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem with role assignment inside delegate component

    Yes. It has to also include the index of course.
    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.


  7. #7
    Join Date
    Mar 2013
    Posts
    18
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Problem with role assignment inside delegate component

    Of course

    Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 10th August 2012, 20:41
  2. Replies: 0
    Last Post: 3rd February 2012, 03:05
  3. Wrap a static QT library inside an XPCOM component ?
    By Paul_Xul in forum Qt Programming
    Replies: 2
    Last Post: 3rd August 2010, 08:16
  4. Signal from inside a delegate
    By hailflex in forum Newbie
    Replies: 2
    Last Post: 13th December 2009, 11:47
  5. Highlighted text/image inside delegate
    By hailflex in forum Newbie
    Replies: 0
    Last Post: 11th December 2009, 16:36

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
  •  
Qt is a trademark of The Qt Company.