Results 1 to 4 of 4

Thread: QML doesn't set the list model when adding tab dynamically the second time

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QML doesn't set the list model when adding tab dynamically the second time

    I have a tab view and trying to add the same tab dynamically using a button.
    However the second time, QML complains with "qrc:///main.qml:43: TypeError: Type error"

    main.qml
    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Controls 1.1
    3. import QtQuick.Layouts 1.1
    4.  
    5. ApplicationWindow {
    6. visible: true
    7. width: 640
    8. height: 480
    9. title: qsTr("Hello World")
    10. signal tabAdded(variant c)
    11.  
    12. ListModel {
    13. id: toolbarModel
    14.  
    15. ListElement {
    16. partColor: "red"
    17. }
    18. ListElement {
    19. partColor: "blue"
    20. }
    21. }
    22.  
    23. ColumnLayout{
    24. anchors.fill: parent
    25.  
    26. TabView{
    27. visible: true
    28. id:tabview
    29. Layout.fillHeight: true
    30. Layout.fillWidth: true
    31.  
    32. }
    33. Button{
    34. text: "add tab"
    35. onClicked:{
    36.  
    37. console.log("Component.onCompleted " + toolbarModel.get(0).name)
    38.  
    39. var t = tabview.addTab("tab", Qt.createComponent("TabItem.qml"));
    40. console.log("Component.onCompleted b " + t)
    41. if(t != null)
    42. {
    43. t.item.partsModel = toolbarModel
    44. var last = tabview.count-1;
    45. tabview.getTab(last).active = true;
    46. console.log(tabview.getTab(last).item);
    47. }
    48. }
    49. }
    50. Button{
    51. text: "remove tab"
    52. onClicked:{
    53.  
    54. var last = tabview.count-1;
    55. tabview.getTab(last).active = false;
    56. console.log(tabview.getTab(last).item);
    57. tabview.removeTab(last)
    58. }
    59. }
    60. }
    61.  
    62. }
    To copy to clipboard, switch view to plain text mode 

    TabItem
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. import QtQuick.Controls 1.1
    4.  
    5. Item{
    6. signal tabButtonClicked()
    7.  
    8. property alias partsModel:parts.partList
    9.  
    10. anchors.fill: parent
    11. Rectangle{
    12. id:colorRect
    13. height: 100
    14. width: 100
    15. z:23
    16. anchors.top : parent.top
    17. color: "red"
    18. }
    19.  
    20. PartList{
    21. id : parts
    22. anchors.left:colorRect.right
    23. z:25
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    Partlist
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item {
    4.  
    5. property var partList
    6.  
    7. ListView{
    8. model:partList
    9. height: 200
    10. width: 100
    11. delegate: delegateComp
    12. }
    13.  
    14. Component{
    15. id: delegateComp
    16. Rectangle{
    17. id: rect
    18. height: 25
    19. width: 25
    20. color: partColor
    21.  
    22. MouseArea{
    23. anchors.fill: parent
    24. onClicked: {
    25. console.log("Color " + partColor)
    26. }
    27. }
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Kindly advice what step I'm missing

  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: QML doesn't set the list model when adding tab dynamically the second time

    First thing I would try is to only call createComponent once.

    Either make a property somewhere, e.g. in the button
    Qt Code:
    1. Component tabComponent: Qt.createComponent("TabItem.qml");
    To copy to clipboard, switch view to plain text mode 
    and use that with insertTab() or use a Component element
    Qt Code:
    1. Component {
    2. id: tabComponent
    3. TabItem {}
    4. }
    To copy to clipboard, switch view to plain text mode 
    and use that.

    Another thing to check is of t.item is valid before you are trying to access it.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    volcano (9th March 2016)

  4. #3
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QML doesn't set the list model when adding tab dynamically the second time

    The component gets created as the reference is printed properly.

    I learned that the tab need to be active before setting the model to the new tab.

    Qt Code:
    1. if(t != null)
    2. {
    3. var last = tabview.count-1;
    4. tabview.getTab(last).active = true;
    5. t.item.partsModel = toolbarModel
    6. console.log(tabview.getTab(last).item);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for the pointers.

  5. #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: QML doesn't set the list model when adding tab dynamically the second time

    Tab is a Loader
    "active" is the Loader's property that tells the Loader whether it should have the item loaded or not.
    So it seems it started in unloaded state.

    You could also just write
    Qt Code:
    1. t.active = true:
    To copy to clipboard, switch view to plain text mode 
    i.e. no need to get through getTab()

    Cheers,
    _

Similar Threads

  1. Dynamically adding resources
    By atomic in forum Qt Programming
    Replies: 10
    Last Post: 21st July 2015, 19:08
  2. Replies: 6
    Last Post: 25th June 2015, 11:54
  3. Adding derived objects to a list dynamically
    By alizadeh91 in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2013, 15:59
  4. Replies: 1
    Last Post: 23rd April 2011, 18:33
  5. Dynamically adding tabs
    By larry104 in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2006, 21:27

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.