Results 1 to 3 of 3

Thread: Can not get check box state from model

  1. #1
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3

    Question Can not get check box state from model

    Hello All,
    There's a problem when I use modelcheckbox.png/delgate in a listview.
    I use a ListView to show some elements.

    In this list view, I define a model and a delegate. In the view, I combine "Enabled" of model to the checkbox in the listView. But the Enabled do not update when check box state change. I do not know how to sync the model state when view changes, can anyone give me some suggestion?

    Code as bellow:
    Qt Code:
    1. ListView {
    2. id: listControler
    3. width: 110
    4. height: 160
    5. anchors.top: parent.top// è¿™å¥è¯ä¼šè®©å…ƒç´ å‘ä¸Šå¯¹é½ï¼Œå¦åˆ™æ˜¯å±…ä¸*对齐
    6. anchors.topMargin: 0
    7. Layout.fillWidth: true
    8. Layout.fillHeight: true
    9. clip:true // 设置为false会导致数据滑出窗外
    10.  
    11. model: ListModel {
    12. id: listControlerModel
    13. ListElement {
    14. ID: "1"
    15. type:"EVC41"
    16. SN: "12345"
    17. Enabled:true
    18. colorCode: "black" // grey(off line) black(Not Check),pink(busy) red(Program Failed) green(Program OK)
    19. currentProgress:0
    20. }
    21. ListElement {
    22. ID: "2"
    23. type:"EVC61"
    24. SN: "12346"
    25. Enabled:true
    26. colorCode: "green" // grey(off line) black(Not Check) pink( busy )red(Program Failed) green(Program OK)
    27. currentProgress:100
    28. }
    29. ListElement {
    30. ID: "3"
    31. type:"Unknow"
    32. SN: "Unknow"
    33. Enabled:false
    34. colorCode: "pink" // grey(off line) black(Not Check) red(Program Failed) green(Program OK)
    35. currentProgress:50
    36. }
    37. }
    38. Component {
    39. id: listControlerHead
    40.  
    41. Row {
    42. spacing: 5
    43. Text {
    44. width: 40
    45. height: 40
    46. text: "升级"
    47.  
    48. }
    49. Text {
    50. width: 40
    51. text: "地址"
    52. font.bold: true
    53. }
    54.  
    55. Text {
    56. width: 80
    57. text: "类型"
    58. font.bold: true
    59. }
    60. Text {
    61. width: 80
    62. text: "SN"
    63. font.bold: true
    64. }
    65. Text {
    66. width: 80
    67. text: "状态"
    68. font.bold: true
    69. }
    70.  
    71. Text {
    72. width: 80
    73. text: "进度"
    74. font.bold: true
    75. }
    76. }
    77. }
    78. header:listControlerHead
    79. delegate: Item {
    80. id: listControlerDelegate
    81. x: 5
    82. width: parent.width
    83. height: 40
    84.  
    85. Row {
    86. spacing: 5
    87. CheckBox {
    88. width: 40
    89. height: 40
    90. checked: Enabled
    91. onCheckedChanged: {
    92. Enabled = checked
    93. }
    94.  
    95. }
    96. Text {
    97. width: 40
    98. text: ID
    99. anchors.verticalCenter: parent.verticalCenter
    100. font.bold: true
    101. }
    102.  
    103. Text {
    104. width: 80
    105. text: type
    106. anchors.verticalCenter: parent.verticalCenter
    107. font.bold: true
    108. }
    109. Text {
    110. width: 80
    111. text: SN
    112. anchors.verticalCenter: parent.verticalCenter
    113. font.bold: true
    114. }
    115. Rectangle {
    116. width: 20
    117. height: 20
    118. anchors.verticalCenter: parent.verticalCenter
    119. color: colorCode
    120. }
    121. ProgressBar {
    122. anchors.verticalCenter: parent.verticalCenter
    123. Layout.fillWidth: true
    124. maximumValue: 100
    125. minimumValue : 0
    126. value: currentProgress
    127. indeterminate: false
    128. }
    129. }
    130. }
    131. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 29th September 2014 at 08:45. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can not get check box state from model

    Is Enabled your custom role ?

    From the Qt docs -
    The names used for roles must begin with a lower-case letter and should be common to all elements in a given model.
    Refer QListElement

  3. #3
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3

    Default Re: Can not get check box state from model

    Yes, Enabled is custom role.
    But it can not update automatictly after I change the all my custom role to start with lower-case.
    To solve this problem, I add bellow code to solve this problem.
    CheckBox {
    width: 40
    height: 40
    checked: programEnabled
    onCheckedChanged: {
    listControlerModel.setProperty(index, "programEnabled", checked)
    }

Similar Threads

  1. Make one qthread check the state of the other
    By kindlychung in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2014, 14:06
  2. Replies: 5
    Last Post: 27th December 2013, 16:27
  3. QAction without check state
    By pardas in forum Newbie
    Replies: 2
    Last Post: 5th August 2012, 12:53
  4. QAbstractItemView and tri state check boxes
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 3rd June 2011, 08:54
  5. Help: How to save Check box state
    By Garibalde in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 21:24

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.