Guest User

Untitled

a guest
Apr 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. TreeView{
  2. id:tree
  3. x: 0
  4. //anchors.fill: parent
  5. width: 335
  6. height: 420
  7. anchors.topMargin: 0
  8. anchors.bottomMargin: 6
  9. anchors.rightMargin: 1287
  10. anchors.bottom: frame.top
  11. anchors.top: parent.top
  12. clip: true
  13.  
  14. model: qjsonmodel
  15. TableViewColumn{
  16. title:"Defects"
  17.  
  18. }
  19.  
  20. }
  21.  
  22. ...
  23.  
  24. class QJsonModel : public QAbstractItemModel
  25. {
  26. Q_OBJECT
  27. public:
  28. explicit QJsonModel(QObject *parent = 0);
  29. ~QJsonModel();
  30. enum JsonRoles{
  31. KeyRole = Qt::UserRole + 1000,
  32. ValueRole
  33. };
  34. ...
  35. QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
  36. ...
  37. };
  38.  
  39. ...
  40. QVariant QJsonModel::data(const QModelIndex &index, int role) const
  41. {
  42. if (!index.isValid())
  43. return QVariant();
  44.  
  45. QJsonTreeItem *item = static_cast<QJsonTreeItem*>(index.internalPointer());
  46.  
  47. if (role == Qt::DisplayRole) {
  48.  
  49. if (index.column() == 0)
  50. return QString("%1").arg(item->key());
  51.  
  52. if (index.column() == 1)
  53. return QString("%1").arg(item->value());
  54. }
  55. else if (role == KeyRole) {
  56. return QString("%1").arg(item->key());
  57. }
  58. else if(role == ValueRole){
  59. return QString("%1").arg(item->value());
  60. }
  61. return QVariant();
  62.  
  63. }
  64. ...
  65. QHash<int, QByteArray> QJsonModel::roleNames() const
  66. {
  67. QHash<int, QByteArray> roles;
  68. roles[KeyRole] = "keyData";
  69. roles[ValueRole] = "valueData";
  70. return roles;
  71. }
  72. ...
  73.  
  74. import QtQuick 2.9
  75. import QtQuick.Window 2.2
  76. import QtQuick.Controls 1.4
  77.  
  78. Window {
  79. visible: true
  80. width: 640
  81. height: 480
  82. title: qsTr("Hello World")
  83.  
  84. TreeView{
  85. anchors.fill: parent
  86.  
  87. model: qjsonmodel
  88. TableViewColumn{
  89. title:"Key"
  90. role: "keyData"
  91. }
  92. TableViewColumn{
  93. title:"Role"
  94. role: "valueData"
  95. }
  96. }
  97. }
Add Comment
Please, Sign In to add comment