Results 1 to 7 of 7

Thread: Selection of Child Checkbox using Header Checkbox and viceversa

  1. #1
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Selection of Child Checkbox using Header Checkbox and viceversa

    Hi Guyz
    I am struck with this simple logic in assigning selected child checkbox values to header checkbox values.This requiremnet is as simple as we see in our daily life i.e., When i select/unselect Header Checkbox all child checkboxes has to be selected/unselected. I could do the above requiremnet. Going further ,when i deselect any of the child checkbox ,header checkbox has to be deselected.I could not achieve the same.Can you guys help me in achieving this as i am struggling for few days. Here i add my source code which i am able to select/deselect child checkbox wen i change header checkbox accordingly.



    Qt Code:
    1. Rectangle{
    2. id: header
    3. width: 620
    4. height: 70
    5. y: 150
    6. color: "#E8E8E8"
    7. anchors{left: parent.left;leftMargin: 110;top:separator.bottom}
    8.  
    9. CheckBox {
    10. id: headerCombo
    11. text: qsTr("Select All")
    12. checked: false
    13. anchors{left: parent.left;leftMargin: 38;top:parent.top;topMargin: 20}
    14.  
    15. onCheckedChanged: {
    16. for(var i=0;i<checkBoxModel.count;i++)
    17. {
    18. checkBoxModel.set(i,{"selected": headerCombo.checked})
    19. }
    20. }
    21. }
    22. }
    23.  
    24. ListModel {
    25. id: checkBoxModel
    26. ListElement { selected: false; }
    27. ListElement { selected: false; }
    28. ListElement { selected: false; }
    29. ListElement { selected: false; }
    30. ListElement { selected: false; }
    31. ListElement { selected: false; }
    32. }
    33.  
    34.  
    35. ListView{
    36. id: listt
    37. model: checkBoxModel
    38. clip:true
    39. width: 620
    40. anchors{left: parent.left;leftMargin: 110;top: header.bottom;topMargin: 10;bottom: bottomBar.top}
    41. delegate: listDelegate
    42. }
    43.  
    44. Component {
    45. id: listDelegate
    46. Item {
    47. id: name
    48. width: 620
    49. height: 70
    50.  
    51. CheckBox {
    52. id: childCombo
    53. text: qsTr("Select All")
    54. checked: selected
    55. anchors{left: parent.left;leftMargin: 38;top:parent.top;topMargin: 20}
    56. }
    57. }
    58. }
    To copy to clipboard, switch view to plain text mode 



    Regards
    Bala Beemaneni

  2. #2
    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: Selection of Child Checkbox using Header Checkbox and viceversa

    So checking/unchecking all items when clicking the header checkbox works?

    What did you do next? Add an onCheckedChanged handler to the delegate's checkbox?
    If so it seems to be missing in your code snippet.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Selection of Child Checkbox using Header Checkbox and viceversa

    Hi
    I have done that...
    onCheckedChanged() //on delegate checkbix
    {
    //here i am changing the header checkbox state. The moment i change header checkbox again its oncheckedChanged is emitted.so all the properties of list is set accordingly
    }


    Regards
    Bala B


    Added after 7 minutes:


    Hi
    Here i post my entire new code which i could get the requirement done with some issues (not sure is it binding issue)

    Here in this code i was able to select/unselect using header and when any child element gets changed header gets deselected.When all the child elements are selected header gets selected.But the issue here is once child elements gets changed its state ,header is not able to set the state back,but when you click on header it successfully updates the child elements list model.I verified the values.But the checkbox front end is not getting selected/unselected..

    Qt Code:
    1. Rectangle{
    2. id: header
    3. width: 620
    4. height: 70
    5. y: 150
    6. color: "#E8E8E8"
    7. anchors{left: parent.left;leftMargin: 110;top:separator.bottom}
    8.  
    9. CheckBox {
    10. id: headerCombo
    11. text: qsTr("Select All")
    12. checked: false
    13. anchors{left: parent.left;leftMargin: 38;top:parent.top;topMargin: 20}
    14. onCheckedChanged: {
    15.  
    16. if(headerCombo.pressed)
    17. {
    18. console.log("checjed chaneging")
    19. for(var i=0;i<checkBoxModel.count;i++)
    20. {
    21. checkBoxModel.set(i,{"selected": headerCombo.checked})
    22. }
    23. }
    24. }
    25. }
    26. }
    27. ListModel {
    28. id: checkBoxModel
    29. ListElement { selected: false; }
    30. ListElement { selected: false; }
    31. ListElement { selected: false; }
    32. ListElement { selected: false; }
    33. ListElement { selected: false; }
    34. ListElement { selected: false; }
    35. }
    36.  
    37.  
    38. ListView{
    39. id: listt
    40. model: checkBoxModel
    41. clip:true
    42. width: 620
    43. anchors{left: parent.left;leftMargin: 110;top: header.bottom;topMargin: 10;bottom: bottomBar.top}
    44. delegate: listDelegate
    45. }
    46.  
    47. Component {
    48. id: listDelegate
    49.  
    50. Warnings{ //consider this item as checkbox
    51. id: warningsList
    52. width: 620
    53. color: "#EFF0F0"
    54. childChkBox: selected //this is checked property
    55. onChildChanged: {
    56.  
    57. if(!headerCombo.pressed && childChkBox===true){
    58. console.log(" changed by child true")
    59. checkBoxModel.set(index,{"selected": true})
    60. checkSelection()
    61. }
    62. else if(!headerCombo.pressed && childChkBox===false){
    63. console.log(" changed by child false")
    64. checkBoxModel.set(index,{"selected": false})
    65. checkSelection()
    66. }
    67. else
    68. {
    69. console.log(" changed by Parent")
    70. for(var i=0;i<checkBoxModel.count;i++)
    71. {
    72. checkBoxModel.set(index,{"selected": headerCombo.checked})
    73. }
    74. }
    75.  
    76. // function checkSelection1()
    77. // {
    78.  
    79. // for(var i=0;i<checkBoxModel.count;i++)
    80. // {
    81. // if(checkBoxModel.get(i).selected===true){
    82. // headerValue=true
    83. // }
    84. // else
    85. // headerValue=false
    86.  
    87. // }
    88.  
    89.  
    90. // }
    91.  
    92.  
    93.  
    94. function checkSelection()
    95. {
    96. if(checkBoxModel.get(0).selected===true)
    97. {
    98. if(checkBoxModel.get(1).selected===true)
    99. {
    100. if(checkBoxModel.get(2).selected===true)
    101. {
    102. if(checkBoxModel.get(3).selected===true)
    103. {
    104. if(checkBoxModel.get(4).selected===true)
    105. {
    106. if(checkBoxModel.get(5).selected===true)
    107. {
    108. headerValue=true
    109. }
    110. else
    111. {
    112. headerValue=false
    113. }
    114. }
    115. else
    116. {
    117. headerValue=false
    118. }
    119. }
    120. else
    121. {
    122. headerValue=false
    123. }
    124. }
    125. else
    126. {
    127. headerValue=false
    128. }
    129. }
    130. else
    131. {
    132. headerValue=false
    133. }
    134. }
    135. else
    136. {
    137. headerValue=false
    138. }
    139. }
    140. }
    141. }
    142. }
    To copy to clipboard, switch view to plain text mode 


    Thanks in Advance


    Regards
    Bala B
    Last edited by beemaneni; 26th October 2015 at 09:50.

  4. #4
    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: Selection of Child Checkbox using Header Checkbox and viceversa

    While this is a lot more complicated than necessary, your problem with the "unresponsive" child is that a binding is overwritten.

    Basically your initial delegate state is this
    Qt Code:
    1. CheckBox {
    2. checked: model.selected
    3. }
    To copy to clipboard, switch view to plain text mode 
    which makes the CheckBox follow the model.
    Then you click the checkbox, you end up with
    Qt Code:
    1. CheckBox {
    2. checked: true // or false depending on the previous value
    3. }
    To copy to clipboard, switch view to plain text mode 
    So there is no binding to the model anymore.

    You need to restore the binding after the component has changed its value by itself
    Qt Code:
    1. CheckBox {
    2. checked: model.selected
    3. onCheckedChanged: {
    4. // not sure of ListModel allows to modify items directly, this seems to already work in your code
    5. checkBoxModel.set(model.index, { "selected": checked });
    6.  
    7. // restore binding
    8. checked = Qt.binding(function() { return model.selected; });
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Selection of Child Checkbox using Header Checkbox and viceversa

    I was not so clear ..Let me ask my doubts..
    Why do i need to restore the values as i am able to successfully see it in list model i.e., i am updating listmodel on every delegate item changed.The values are getting updated in listmodel properly.At any point of time i am able to see which is selected/deselected in my list model which is quite good.
    I am not exactly clear what is happening during that binding.If you dont mind , can u elaborate


    Thanks & regards
    Bala B

  6. #6
    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: Selection of Child Checkbox using Header Checkbox and viceversa

    Quote Originally Posted by beemaneni View Post
    Why do i need to restore the values as i am able to successfully see it in list model
    You don't need to restore any values, you need to restore property bindings after they got overwritten.

    Quote Originally Posted by beemaneni View Post
    I am not exactly clear what is happening during that binding.If you dont mind , can u elaborate
    You start with a property binding for the checked property, so it gets updated whenever the model's data changes.
    You then click in the checkbox, so the value is now fixed, no longer a binding.
    If you want the checkbox to update itself based on the model data again, you need to restore the binding.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: [SOLVED]Selection of Child Checkbox using Header Checkbox and viceversa

    Thanks Dude
    That works fine.

Similar Threads

  1. Replies: 5
    Last Post: 30th September 2013, 23:03
  2. Replies: 0
    Last Post: 12th January 2012, 06:41
  3. Replies: 4
    Last Post: 1st June 2011, 14:54
  4. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21:59
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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.