Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML type error when adding tab twice
Qt 6.11 is out! See what's new in the release blog

QML type error when adding tab twice

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valerian
    wrote on last edited by
    #1

    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"

    Here's the code.
    main.qml

    import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Layouts 1.1
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        signal tabAdded(variant c)
    
        ListModel {
            id: toolbarModel
    
            ListElement {
                partColor: "red"
            }
            ListElement {
                partColor: "blue"
            }
        }
    
        ColumnLayout{
            anchors.fill: parent
    
            TabView{
                visible: true
                id:tabview
                Layout.fillHeight: true
                Layout.fillWidth: true
    
            }
            Button{
                text: "add tab"
                onClicked:{
    
                    console.log("Component.onCompleted " + toolbarModel.get(0).name)
    
                    var t = tabview.addTab("tab", Qt.createComponent("TabItem.qml"));
                    console.log("Component.onCompleted b " + t)
                    if(t != null)
                    {
                        t.item.partsModel = toolbarModel
                        var last = tabview.count-1;
                        tabview.getTab(last).active = true; 
                        console.log(tabview.getTab(last).item);
                    }
                }
            }
            Button{
                text: "remove tab"
                onClicked:{
    
                    var last = tabview.count-1;
                    tabview.getTab(last).active = false;
                    console.log(tabview.getTab(last).item);
                    tabview.removeTab(last)
                }
            }
        }
    
    }
    

    Partlist.qml

    import QtQuick 2.0
    
    Item {
    
        property var partList
    
        ListView{
            model:partList
            height: 200
            width: 100
            delegate: delegateComp
        }
    
        Component{
            id: delegateComp
            Rectangle{
                id: rect
                height: 25
                width: 25
                color: partColor
    
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        console.log("Color " + partColor)
                    }
                }
            }
        }
    }
    

    TabItem.qml

    import QtQuick 2.0
    
    import QtQuick.Controls 1.1
    
    Item{
        signal tabButtonClicked()
    
        property alias partsModel:parts.partList
    
        anchors.fill: parent
        Rectangle{
            id:colorRect
            height: 100
            width: 100
            z:23
            anchors.top : parent.top
            color: "red"
        }
    
        PartList{
            id : parts
            anchors.left:colorRect.right
            z:25
        }
    }
    

    Kindly advice what step I'm missing

    1 Reply Last reply
    0
    • William McKIEW Offline
      William McKIEW Offline
      William McKIE
      wrote on last edited by
      #2

      Hi,

      When you append a tab, the content of tab is only loaded when the tab become active.

      if(t !== null)
      {
          var last = tabview.count-1;
          tabview.getTab(last).active = true;
          t.item.partsModel = toolbarModel
          console.log(tabview.getTab(last).item);
       }
      

      Sincerely,
      William

      1 Reply Last reply
      1
      • V Offline
        V Offline
        Valerian
        wrote on last edited by
        #3

        @William-McKIE Yes it works now.. Thanks for guidance.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved