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. when this type of error comes "TypeError: Cannot read property 'width' of null"
Forum Update on Monday, May 27th 2025

when this type of error comes "TypeError: Cannot read property 'width' of null"

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.5k 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 25 Jun 2023, 09:39 last edited by
    #1

    i have implemented below code

    when i remove item on click of clear button i am getting error: TypeError: Cannot read property 'width' of null

    how to resolve it ?

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.5
    import QtQuick.Layouts 1.3
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Dynamic Models Demo")
    
        ListModel{
            id: mListModel
    
            ListElement{
                firstName:"John"; lastName:"Snow";
            }
    
            ListElement{
                firstName:"Mangal"; lastName:"Prajapati";
            }
    
            ListElement{
                firstName:"Mitch"; lastName:"Itchenco";
            }
    
            ListElement{
                firstName:"Ken"; lastName:"Kologorov";
            }
    
            ListElement{
                firstName:"Vince"; lastName:"Luvkyj";
            }
        }
    
        ColumnLayout{
            anchors.fill: parent
            ListView{
                id: mListViewId
                model:mListModel
                delegate:delegateId
                Layout.fillHeight: true
                Layout.fillWidth: true
            }
    
            Button{
                text:"Add Item"
                Layout.fillWidth: true
                onClicked: {
                    mListModel.append({"firstName":"Meghal","lastName":"Prajapati"})
                }
            }
            Button{
                text:"Clear"
                Layout.fillWidth: true
                onClicked: {
                mListModel.clear()
                }
            }
            Button{
                Layout.fillWidth: true
                text:"Delete item at index 2"
                onClicked: {
    
                }
            }
            Button{
                Layout.fillWidth: true
                text:"Set Item at index 2"
                onClicked: {
    
                }
            }
    
        }
    
        Component{
            id: delegateId
            Rectangle{
                id: recID
                width: parent.width
                height: 50
                color: "beige"
                border.color: "yellowgreen"
                radius: 14
    
                Text {
                    id: textID
                    text: firstName + " "+ lastName
                    anchors.centerIn: parent
                    font.pointSize: 20
                }
    
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        print("Clicked on "+firstName + " "+ lastName)
                    }
                }
            }
        }
    }
    
    S 1 Reply Last reply 26 Jun 2023, 09:09
    0
    • Q Qt embedded developer
      25 Jun 2023, 09:39

      i have implemented below code

      when i remove item on click of clear button i am getting error: TypeError: Cannot read property 'width' of null

      how to resolve it ?

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.5
      import QtQuick.Layouts 1.3
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Dynamic Models Demo")
      
          ListModel{
              id: mListModel
      
              ListElement{
                  firstName:"John"; lastName:"Snow";
              }
      
              ListElement{
                  firstName:"Mangal"; lastName:"Prajapati";
              }
      
              ListElement{
                  firstName:"Mitch"; lastName:"Itchenco";
              }
      
              ListElement{
                  firstName:"Ken"; lastName:"Kologorov";
              }
      
              ListElement{
                  firstName:"Vince"; lastName:"Luvkyj";
              }
          }
      
          ColumnLayout{
              anchors.fill: parent
              ListView{
                  id: mListViewId
                  model:mListModel
                  delegate:delegateId
                  Layout.fillHeight: true
                  Layout.fillWidth: true
              }
      
              Button{
                  text:"Add Item"
                  Layout.fillWidth: true
                  onClicked: {
                      mListModel.append({"firstName":"Meghal","lastName":"Prajapati"})
                  }
              }
              Button{
                  text:"Clear"
                  Layout.fillWidth: true
                  onClicked: {
                  mListModel.clear()
                  }
              }
              Button{
                  Layout.fillWidth: true
                  text:"Delete item at index 2"
                  onClicked: {
      
                  }
              }
              Button{
                  Layout.fillWidth: true
                  text:"Set Item at index 2"
                  onClicked: {
      
                  }
              }
      
          }
      
          Component{
              id: delegateId
              Rectangle{
                  id: recID
                  width: parent.width
                  height: 50
                  color: "beige"
                  border.color: "yellowgreen"
                  radius: 14
      
                  Text {
                      id: textID
                      text: firstName + " "+ lastName
                      anchors.centerIn: parent
                      font.pointSize: 20
                  }
      
                  MouseArea{
                      anchors.fill: parent
                      onClicked: {
                          print("Clicked on "+firstName + " "+ lastName)
                      }
                  }
              }
          }
      }
      
      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 26 Jun 2023, 09:09 last edited by
      #2

      @Qt-embedded-developer said in when this type of error comes "TypeError: Cannot read property 'width' of null":

      Rectangle{
      id: recID
      width: parent.width

      You can use layouts here:

      Rectangle {
        id: recID
        Layout.fillWidth: true
      

      That should get rid of the warning. Or, if you want to do it in another way:

      Rectangle {
        id: recID
        width: parent ? parent.width : 0
      

      (Z(:^

      Q 1 Reply Last reply 26 Jun 2023, 12:01
      3
      • S sierdzio
        26 Jun 2023, 09:09

        @Qt-embedded-developer said in when this type of error comes "TypeError: Cannot read property 'width' of null":

        Rectangle{
        id: recID
        width: parent.width

        You can use layouts here:

        Rectangle {
          id: recID
          Layout.fillWidth: true
        

        That should get rid of the warning. Or, if you want to do it in another way:

        Rectangle {
          id: recID
          width: parent ? parent.width : 0
        
        Q Offline
        Q Offline
        Qt embedded developer
        wrote on 26 Jun 2023, 12:01 last edited by
        #3

        @sierdzio said in when this type of error comes "TypeError: Cannot read property 'width' of null":

        Rectangle {
        id: recID
        width: parent ? parent.width : 0

        yes this help me to resolve this error.

        Thank you !!!

        1 Reply Last reply
        0
        • Q Qt embedded developer has marked this topic as solved on 26 Jun 2023, 12:01

        1/3

        25 Jun 2023, 09:39

        • Login

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