Results 1 to 9 of 9

Thread: Using an If condition in a QML delegate?

  1. #1
    Join Date
    Oct 2009
    Posts
    47
    Thanks
    10

    Default Using an If condition in a QML delegate?

    Hi,

    I'm having an issue where I need to draw a different colored Rectangle based on a condition. OR better yet is it possible to use 2 different delegates?

    So for instance
    if for instance I get a value from the model such as "active" then the rectangle I should create should be green.

    If I get "not active" the rectangle will look different.

    THe problem is I don't know how I can use an if condition in the delegate. Currently I have:


    Qt Code:
    1. ListView {
    2. id: calendarListView
    3. x: 7
    4. y: 96
    5. width: 587
    6. height: 418
    7.  
    8. model: callsonholdModel
    9.  
    10. delegate:
    11.  
    12. Rectangle {
    13. id: calendarCellHolder
    14. height: 124
    15. border.width: 2
    16. border.color: "#CFCFCF"
    17. width: calendarHeader.width
    18.  
    19. //HERE IS WHERE I NEED TO DECIDE WHAT RECTANGLE TO DRAW. CURRENTLY ITS JUST DRAWING ALL THE SAME RECTANGLE.
    20.  
    21. Rectangle {
    22. id: activeMeetingRect
    23.  
    24. width: calendarHeader.width - 30
    25. height: 84
    26. radius: 9
    27.  
    28. anchors.top:calendarCellHolder.top
    29. anchors.right: calendarCellHolder.right
    30. anchors.left: calendarCellHolder.left
    31.  
    32. anchors.topMargin: 20
    33. anchors.bottomMargin: 20
    34. anchors.leftMargin: 15
    35. anchors.rightMargin: 15
    36.  
    37. gradient: Gradient {
    38. GradientStop {
    39. position: 0
    40. color: "#5ffb26"
    41. }
    42.  
    43. GradientStop {
    44. position: 1
    45. color: "#000000"
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 



    EDIT: I actually just found out I could wrap the Rectangle's with a component { id: Delegate1 Rect{...} } component { id: Delegate2 Rect{...} }

    so really I just need to see if I can use an if/else inside the delegate to switch between Delegate1 and Delgate2.


    Thanks
    Last edited by technoViking; 9th November 2010 at 02:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using an If condition in a QML delegate?

    As far as I know, you can use javascript.

  3. #3
    Join Date
    Nov 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: Using an If condition in a QML delegate?

    Hi,

    I am struggling with the same issue of delegating different elements by if. Please share some insight if you got anything on this.

    Thanks in advance.
    Kunal

  4. #4
    Join Date
    May 2010
    Posts
    61
    Thanks
    2
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using an If condition in a QML delegate?

    Hi Guys,

    Does anyone have an update on this one?
    Any direction is highly appreciated.

    Thanks and regards,
    Wladek
    One second is long, everything longer than two seconds is definitely too long.

  5. #5
    Join Date
    May 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using an If condition in a QML delegate?

    Bump for an answer to this, I would really like this functionality


    Added after 22 minutes:


    hhurtta in #qt-qml on irc had the answer. Use a Loader in the component:

    Qt Code:
    1. import Qt 4.7
    2.  
    3. Rectangle {
    4. width: 640
    5. height: 480
    6.  
    7.  
    8. ListView {
    9. id: view
    10. anchors.fill: parent
    11. model: model
    12. delegate: delegate
    13.  
    14. }
    15.  
    16. ListModel {
    17. id: model
    18. ListElement {
    19. name: "text"
    20. value: "hello"
    21. }
    22. ListElement {
    23. name: "bool"
    24. value: true
    25. }
    26. ListElement {
    27. name: "bool"
    28. value: false
    29. }
    30. ListElement {
    31. name: "text"
    32. value: "world"
    33. }
    34. }
    35.  
    36. Component {
    37. id: textComponent
    38. Text {
    39. text: value
    40. }
    41. }
    42.  
    43.  
    44. Component {
    45. id: boolComponent
    46. Text {
    47. text: value ? "[X]" : "[ ]"
    48. }
    49. }
    50.  
    51. Component {
    52. id: delegate
    53.  
    54. Loader {
    55. sourceComponent: name == "text" ? textComponent : boolComponent
    56. }
    57.  
    58. }
    59.  
    60. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by malcom2073; 14th March 2011 at 13:50.

  6. #6
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: Using an If condition in a QML delegate?

    I have tried the malcom2073's if-example on Qt 4.7.0 and it works as expected. However on 4.7.2 the model variables are not visible to the textComponent and boolComponent delegates:
    Qt Code:
    1. TestDelegates.qml:39: ReferenceError: Can't find variable: value
    2. TestDelegates.qml:39: ReferenceError: Can't find variable: value
    To copy to clipboard, switch view to plain text mode 

    I have also tried to change the name of the variable in case it may be reserved keyword with the same result.

    Does anyone have an idea what has changed from 4.7.0 to 4.7.2 in this topic and how to update this example to work?

    Regards,
    Darek

  7. #7
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using an If condition in a QML delegate?

    You can use conditional expressions for the properties. That's how I achieve the desired behaviour.

    e.g.

    Rectangle {
    radius: activeMeeting ? 4 : 0; // activeMeeting is a field in your particular model.
    }

  8. #8
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: Using an If condition in a QML delegate?

    Quote Originally Posted by fgungor View Post
    You can use conditional expressions for the properties. That's how I achieve the desired behaviour.
    Yes, actually I end up with something similar. Instead of multiple conditional delegates I use one delegate with several states - each of them sets up the delegate to look in a different way. Based on some properties values I pick a state and assign it to the delegate.

  9. #9
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using an If condition in a QML delegate?

    Quote Originally Posted by isdes View Post
    I have tried the malcom2073's if-example on Qt 4.7.0 and it works as expected. However on 4.7.2 the model variables are not visible to the textComponent and boolComponent delegates:
    Qt Code:
    1. TestDelegates.qml:39: ReferenceError: Can't find variable: value
    2. TestDelegates.qml:39: ReferenceError: Can't find variable: value
    To copy to clipboard, switch view to plain text mode 

    I have also tried to change the name of the variable in case it may be reserved keyword with the same result.

    Does anyone have an idea what has changed from 4.7.0 to 4.7.2 in this topic and how to update this example to work?

    Regards,
    Darek
    Same here with 4.7.2 - but found another solution - one Component with a conditional state switch:

    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. width: 640
    5. height: 480
    6.  
    7.  
    8. ListView {
    9. id: view
    10. anchors.fill: parent
    11. model: model
    12. delegate: delegate
    13. }
    14.  
    15. ListModel {
    16. id: model
    17.  
    18. ListElement {
    19. name: "text"
    20. value: "hello"
    21. asd: 't'
    22. }
    23.  
    24. ListElement {
    25. name: "bool"
    26. value: true
    27. qwe: 'b'
    28. }
    29. ListElement {
    30. name: "bool"
    31. value: false
    32. qwe: 'b'
    33. }
    34. ListElement {
    35. name: "text"
    36. value: "world"
    37. asd: 't'
    38. }
    39. }
    40.  
    41. Component {
    42. id: delegate
    43. Rectangle {
    44. id: wgt
    45.  
    46. property alias r1_visible: r1.visible
    47. property alias r2_visible: r2.visible
    48.  
    49. height: 20
    50. state: name == "text" ? "s1" : "s2"
    51.  
    52. Rectangle {
    53. id: r1
    54. Text {
    55. id: c1
    56. text: asd + " " + value
    57. }
    58. }
    59.  
    60. Rectangle {
    61. id: r2
    62. Text {
    63. id: c2
    64. text: qwe + " " + (value ? "[X]" : "[ ] ")
    65. }
    66. }
    67.  
    68. states: [
    69. State {
    70. name: "s1"
    71. PropertyChanges {
    72. target: wgt
    73. r1_visible: true
    74. r2_visible: false
    75. }
    76. },
    77. State {
    78. name: "s2"
    79. PropertyChanges {
    80. target: wgt
    81. r1_visible: false
    82. r2_visible: true
    83. }
    84. }
    85.  
    86. ]
    87. }
    88. }
    89. }
    To copy to clipboard, switch view to plain text mode 

    It still feels a bit hacky and I think that this is a bug in 4.7.2 but everything looks as expected

    Qt Code:
    1. t hello
    2. b [X]
    3. b [ ]
    4. t world
    To copy to clipboard, switch view to plain text mode 

    and even no warning about missing properties (asd/qwe)

Similar Threads

  1. Replies: 2
    Last Post: 30th June 2010, 10:48
  2. Is delegate necessary here?
    By scythe in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 19:59
  3. Replies: 4
    Last Post: 5th March 2010, 18:03
  4. Sleep condition in QT
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2008, 12:07
  5. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 20:28

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.