Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 3.59 KB | None | 0 0
  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 2.12
  4. import QtQml.Models 2.12
  5.  
  6. ApplicationWindow {
  7.     id: root
  8.     width: 800
  9.     height: 480
  10.     visible: true
  11.     title: qsTr("Hello World")
  12.  
  13.     property bool isGridStyle: enableGrid.checked
  14.  
  15.     header: ToolBar {
  16.         ToolButton {
  17.             id: enableGrid
  18.             text: checked ? "To List" : "To Grid"
  19.             checkable: true
  20.         }
  21.     }
  22.  
  23.     ListModel {
  24.         id: sourceModel
  25.         ListElement {
  26.             name: "test1"
  27.             color: "red"
  28.         }
  29.         ListElement {
  30.             name: "test2"
  31.             color: "green"
  32.         }
  33.         ListElement {
  34.             name: "test1"
  35.             color: "blue"
  36.         }
  37.     }
  38.  
  39.     Component {
  40.         id: cellComponent
  41.         Rectangle {
  42.             id: cell
  43.             width: root.isGridStyle ? 50 : 100
  44.             height: root.isGridStyle ? 50 : 34
  45.  
  46.             Behavior on width {
  47.                 NumberAnimation { duration: 1000 }
  48.             }
  49.             Behavior on height {
  50.                 NumberAnimation { duration: 1000 }
  51.             }
  52.             Behavior on x {
  53.                 NumberAnimation { duration: 1000 }
  54.             }
  55.             Behavior on y {
  56.                 NumberAnimation { duration: 1000 }
  57.             }
  58.  
  59.             color: model.index % 2 ? "lightgray" : "lightblue"
  60.             function updateAnchors() {
  61.                 if (root.isGridStyle) {
  62.                     colorRect.anchors.verticalCenter = undefined
  63.                     colorRect.anchors.left = undefined
  64.                     nameLabel.anchors.verticalCenter = undefined
  65.                     nameLabel.anchors.left = undefined
  66.  
  67.                     colorRect.anchors.horizontalCenter = cell.horizontalCenter
  68.                     colorRect.anchors.top = cell.top
  69.                     nameLabel.anchors.horizontalCenter = cell.horizontalCenter
  70.                     nameLabel.anchors.top = colorRect.bottom
  71.                 } else {
  72.                     colorRect.anchors.horizontalCenter = undefined
  73.                     colorRect.anchors.top.horizontalCenter = undefined
  74.                     nameLabel.anchors.horizontalCenter = undefined
  75.                     nameLabel.anchors.top = undefined
  76.  
  77.                     colorRect.anchors.verticalCenter = cell.verticalCenter
  78.                     colorRect.anchors.left = cell.left
  79.                     nameLabel.anchors.verticalCenter = cell.verticalCenter
  80.                     nameLabel.anchors.left = colorRect.right
  81.                 }
  82.             }
  83.             Component.onCompleted: updateAnchors()
  84.             Connections {
  85.                 target: root
  86.                 onIsGridStyleChanged: {
  87.                     updateAnchors()
  88.                 }
  89.             }
  90.             Rectangle {
  91.                 id: colorRect
  92.                 color: model.color
  93.                 width: 32
  94.                 height: 32
  95.                 anchors.margins: 2
  96.             }
  97.             Label {
  98.                 id: nameLabel
  99.                 anchors.margins: 2
  100.                 text: model.name
  101.             }
  102.  
  103.         }
  104.     }
  105.  
  106.     DelegateModel {
  107.         id: cellsModel
  108.         model: sourceModel
  109.         delegate: cellComponent
  110.     }
  111.  
  112.     Component {
  113.         id: listViewComponent
  114.         ListView {
  115.             model: cellsModel
  116.         }
  117.     }
  118.  
  119.     Component {
  120.         id: gridViewComponent
  121.         GridView {
  122.             model: cellsModel
  123.         }
  124.     }
  125.  
  126.     Loader {
  127.         anchors.fill: parent
  128.         sourceComponent: isGridStyle ? gridViewComponent : listViewComponent
  129.     }
  130.  
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement