Results 1 to 4 of 4

Thread: Can Repeater delegate in Qml be made to behave in a generic way to the given Items?

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

    Default Can Repeater delegate in Qml be made to behave in a generic way to the given Items?

    {quick 2.0, Qml Qt 5.1 beta}

    I wanted to know if such an idiom would be possible in Qml: Below I have objLeftColumn which expects its children to expose a boolean m_bIsSelected and a MouseArea alias m_mouseProperty and uses them to make the collection of such children mutually exclusive, ie., only one of them can be in selected state. The followin works fine but I need to repeat it every time I want and specially if I wanted it for Row etc.
    Qt Code:
    1. Column {
    2. id: objLeftColumn
    3.  
    4. property int m_iLastButtonClicked: -1
    5. property int m_iCurrentButtonClicked: -1
    6.  
    7. onM_iCurrentButtonClickedChanged: {
    8. if(m_iLastButtonClicked != -1) {
    9. objLeftColumn.children[m_iLastButtonClicked].m_bIsSelected = false
    10. }
    11. m_iLastButtonClicked = m_iCurrentButtonClicked
    12. }
    13.  
    14. Repeater {
    15. id: objLeftColumnRepeater
    16.  
    17. model: 5
    18.  
    19. delegate: ABCD {
    20. id: objABCD
    21.  
    22. m_mouseProperty.onClicked: {
    23. if(m_bIsSelected) {
    24. objLeftColumn.m_iCurrentButtonClicked = index
    25. }
    26. else {
    27. objLeftColumn.m_iLastButtonClicked = -1
    28. objLeftColumn.m_iCurrentButtonClicked = -1
    29. }
    30. }
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    Can I write a generic objLeftColumn (in a separate qml file) that could arrange the given Items in Column while aslo dealing with exclusivity of their selection?

    The idea is instead of giving the component to the delegate right there an then, I'll give it later and for each instantiation of the component (depending on numeric value of model above and below) the delegate: in Repeater should behave similarly.

    eg., in psedo code:

    in Exclusive.qml:
    Qt Code:
    1. Column {
    2. id: objLeftColumn
    3.  
    4. property int m_iLastButtonClicked: -1
    5. property int m_iCurrentButtonClicked: -1
    6.  
    7. property alias m_delegate: objLeftColumnRepeater.delegate
    8.  
    9. onM_iCurrentButtonClickedChanged: {
    10. if(m_iLastButtonClicked != -1) {
    11. objLeftColumn.children[m_iLastButtonClicked].m_bIsSelected = false
    12. }
    13. m_iLastButtonClicked = m_iCurrentButtonClicked
    14. }
    15.  
    16. Repeater {
    17. id: objLeftColumnRepeater
    18.  
    19. model: 5
    20. onItemAdded: {
    21. //state of item can be manipulated but want to
    22. //add behaviour to the item (like accessing a function through virtual mechanism in c++) eg:
    23. /*item {
    24.   m_mouseProperty.onClicked: {
    25.   //do something
    26.   }
    27.   }*/
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    in SomeOther.qml:
    Qt Code:
    1. Exclusive {
    2. m_delegate: ABCD
    3. }
    4.  
    5. Exclusive {
    6. m_delegate: DEFG
    7. }
    To copy to clipboard, switch view to plain text mode 

    etc..So this way Column in Exclusive is more generic and can be called with any Item assigned to its (Repeater's) delegate: and will behave similarly. Requirements: the item should expose m_mouseProperty (alias for MouseArea) and m_bIsSelected (bool) property.
    Last edited by ustulation; 18th June 2013 at 06:59.

  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: Can Repeater delegate in Qml be made to behave in a generic way to the given Item

    Shouldn't something like that work?

    javascript Code:
    1. Column {
    2. id: col
    3. property int selectedIndex: -1
    4.  
    5. Repeater {
    6. model: 5
    7. Rectangle {
    8. width: 200; height: 50
    9. color: col.selectedIndex == index ? "red" : "white"
    10. MouseArea {
    11. anchors.fill: parent
    12. onClicked: col.selectedIndex = index
    13. }
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    You can easily create a component from it like so:

    javascript Code:
    1. Column {
    2. id: col
    3. property alias delegate: rep.delegate
    4. property alias model: rep.model
    5. property int selectedIndex: -1
    6.  
    7. Repeater {
    8. id: rep
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    If you really want to, you can adjust the delegate to add a MouseArea to the delegate to have everything contained in a single component, probably more or less like so:

    javascript Code:
    1. Column {
    2. id: col
    3. property Component delegate
    4. property alias model: rep.model
    5. property int selectedIndex: -1
    6.  
    7. Repeater {
    8. id: rep
    9. }
    10. Component {
    11. id: macmp
    12. MouseArea {
    13. anchors.fill: parent
    14. onClicked: col.selectedIndex = index
    15. }
    16. }
    17. onDelegateChanged: {
    18. var obj = delegate.createInstance(rep) // or create the item elsewhere and assign it to the rep.delegate
    19. var ma = macmp.createInstance(obj)
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th June 2013 at 08:08.
    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
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default Re: Can Repeater delegate in Qml be made to behave in a generic way to the given Item

    no that part is clear to me. As I wrote in the comments to the code manipulating the delegate item's state is easy. How do you add a behaviour? eg.,

    Qt Code:
    1. SomeComponent {
    2. m_mouseProperty.onClicked: {
    3. //add behaviour
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    here in SomeComponent.qml the component could already have implemented some behaviours on onClicked. What I believe I've done above is added to that behaviour so that in addition to all that it was doing on onClicked it will also do whatever it is asked to in above code. The question is can I somehow achieve this in delegate of a repeater (of a Column in this eg.):

    Generic.qml
    Qt Code:
    1. Column {
    2. property alias m_delegate: objRepeater.delegate
    3. property alias m_count: objRepeater.model
    4. Repeater {
    5. id: objRepeater
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now if I use Generic.qml it should add a behaviour to the component supplied as a delegate:
    Qt Code:
    1. Generic {
    2. m_delegate: SomeItemWith_m_mouseProperty
    3. m_count: 5
    4. }
    To copy to clipboard, switch view to plain text mode 

    now Generic should be able to add behaviour to m_mouseProperty (which is an alias to MouseArea in this eg.)

    Qt Code:
    1. onM_delegateChanged: {
    2. children[index].m_mouseProperty.onClicked: {
    3. //Add behaviour...ie., Do something
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    this ofcourse gives an error. So i can modify the state (children[index].m_someProperty = false is perfectly legal) but can I also add behaviour (say on onClicked) like in the example above?

  4. #4
    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: Can Repeater delegate in Qml be made to behave in a generic way to the given Item

    I don't understand what you mean. If you want to stack mouse areas then you can do that. If one area ignores an event, an underlying area will get it, etc.
    If you want a series of functions to be executed as a result of an event then it should be possible as well -- you can create an array of javascript functions and have something execute them one after the other.
    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. set combobox delegate on some view items
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 8th March 2011, 14:36
  2. Replies: 0
    Last Post: 30th August 2010, 19:08
  3. QTreeView different item delegate for child items possible?
    By Royceybaby in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2010, 21:14
  4. Replies: 0
    Last Post: 4th April 2009, 15:54
  5. Use delegate to draw different type of items
    By nifei in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2009, 13:16

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.