Results 1 to 9 of 9

Thread: Using an If condition in a QML delegate?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.
    }

  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?

    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.

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