TableView: ReferenceError: styleData is not defined
-
Hello!
I wrote this code :import QtQuick import QtQuick.Window import QtQuick.Controls import QtQuick.Layouts import Qt.labs.qmlmodels ... TableView { id: tableView ... model: myModel delegate: Rectangle { implicitWidth: 100 implicitHeight: 50 border.width: 1 Text { text: styleData.Value anchors.centerIn: parent onTextChanged: console.log(styleData.row) } } }An error appears when launching this application:
"ReferenceError: styleData is not defined".The Qt version is 6.8.2.
I can't find anything about this error.
Has anyone come across this?
Thanks in advance! -
It looks like you are basing your code on an example for the Quick Controls 1 version of
TableView, which was deprecated in the final releases of Qt 5 and was removed entirely from Qt 6. The version ofTableViewthat exists in Qt 6 is a completely different implementation and does not usestyleData. -
It looks like you are basing your code on an example for the Quick Controls 1 version of
TableView, which was deprecated in the final releases of Qt 5 and was removed entirely from Qt 6. The version ofTableViewthat exists in Qt 6 is a completely different implementation and does not usestyleData.@Bob64 said in TableView: ReferenceError: styleData is not defined:
which was deprecated in the final releases of Qt 5
Yes, it is.
But I need a different appearance of the cells depending on the column or role.
How do I do this in Qt 6?
I found the DelegateChoice class:
https://doc.qt.io/qt-6.8/qml-qt-labs-qmlmodels-delegatechoice.html .
Is that what I need? -
It depends how different your delegates need to be.
DelegateChoiceis good for cases where you need fundamentally different delegates depending on the role etc. If it is just some small variations in appearance that you need, such as a different colour of something, you might just be able to code such variation directly in a single delegate. Note that in Qt 6TableViewthings likeindex,row,columnas well as the role values are exposed to the delegate directly as properties.As a trivial example of what I mean about coding the logic directly in the delegate:
delegate: Rectangle { ... color: column % 2 ? "red" : "blue" } -
It depends how different your delegates need to be.
DelegateChoiceis good for cases where you need fundamentally different delegates depending on the role etc. If it is just some small variations in appearance that you need, such as a different colour of something, you might just be able to code such variation directly in a single delegate. Note that in Qt 6TableViewthings likeindex,row,columnas well as the role values are exposed to the delegate directly as properties.As a trivial example of what I mean about coding the logic directly in the delegate:
delegate: Rectangle { ... color: column % 2 ? "red" : "blue" }@Bob64 Thanks for the detailed reply!
-
W woodpecker has marked this topic as solved on