when this type of error comes "TypeError: Cannot read property 'width' of null"
-
wrote on 25 Jun 2023, 09:39 last edited by
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) } } } } }
-
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) } } } } }
@Qt-embedded-developer said in when this type of error comes "TypeError: Cannot read property 'width' of null":
Rectangle{
id: recID
width: parent.widthYou 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
-
@Qt-embedded-developer said in when this type of error comes "TypeError: Cannot read property 'width' of null":
Rectangle{
id: recID
width: parent.widthYou 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
wrote on 26 Jun 2023, 12:01 last edited by@sierdzio said in when this type of error comes "TypeError: Cannot read property 'width' of null":
Rectangle {
id: recID
width: parent ? parent.width : 0yes this help me to resolve this error.
Thank you !!!
-
1/3