Results 1 to 2 of 2

Thread: Change the ListModel dynamically

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Change the ListModel dynamically

    Qt Code:
    1. import QtQuick 2.1
    2. import QtQuick.Controls 1.0
    3.  
    4. Rectangle {
    5. id: root
    6. width: 500
    7. height: 500
    8.  
    9. state: "one"
    10.  
    11. ListModel{
    12. id: one
    13. }
    14.  
    15. ListModel{
    16. id: two
    17. }
    18.  
    19. function changeModel(){
    20. if(state == "one"){
    21. one.append({"source": "aaa"})
    22. return one
    23. }else{
    24. two.append({"source": "bbb"})
    25. return two
    26. }
    27. }
    28.  
    29. Row{
    30. Button{
    31. id: buttonOne
    32.  
    33. text: "One"
    34.  
    35. onClicked: {
    36. root.state = "one"
    37. }
    38. }
    39.  
    40. Button{
    41. id: buttonTwo
    42.  
    43. text: "Two"
    44.  
    45. onClicked: {
    46. root.state = "two"
    47. }
    48. }
    49.  
    50. TableView{
    51. id: tableView
    52. model: changeModel()
    53.  
    54. TableViewColumn {
    55. title: "image"
    56.  
    57. delegate: Text{ text: tableView.model.get(styleData.row).source }
    58. }
    59. }
    60. }
    61.  
    62. }
    To copy to clipboard, switch view to plain text mode 

    When I click the “Two” button, there are always an error message

    qrc:/main.qml:59: TypeError: Cannot read property ‘source’ of undefined

    How could I change the model dynamically?What kind of mistake I make?Thanks

  2. #2
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the ListModel dynamically

    Got the answer from here
    http://qt-project.org/forums/viewthread/29653/

    The reason is that you change your underlaying model of the TableView and both are not of the same length.
    Since you use .get(styleData.row) all the delegates visible in the table refer to a specific element on index (row number) in you model.
    If you change the model, each delegate will try to get its data again, but the number of items of the other model is different, and if the number of items is less than the items that are visible, some of the items will fail to get its data, those rows will dissappear but give an error.
    If you use:

    #
    delegate: Text{ text: styleData.value }

    instead, then it works, without errors, but then you cannot have multiple properties in the same ListElement.

    Hope it helps.

Similar Threads

  1. Dynamically change slider max range
    By Don_Hannes in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 16:19
  2. Dynamically change existing curve color
    By missoni in forum Qwt
    Replies: 2
    Last Post: 19th June 2012, 11:32
  3. Replies: 3
    Last Post: 10th October 2011, 08:44
  4. Dynamically change the layout
    By pippo42 in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 13:01
  5. Replies: 4
    Last Post: 16th March 2009, 09:08

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.