Results 1 to 2 of 2

Thread: Moving items of qabstractitemmodel in qml view

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Moving items of qabstractitemmodel in qml view

    Hi all,

    How can I move an item from qabstractitemmodel in a qml view?

    Kindly advice.

  2. #2
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving items of qabstractitemmodel in qml view

    Here's the code

    This is the TreeModel
    Qt Code:
    1. class TreeModel : public QAbstractItemModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit TreeModel(const QString &data, QObject *parent = 0);
    7. ~TreeModel();
    8.  
    9. enum TreeRoles {
    10. TitleRole = Qt::UserRole + 1,
    11. SummaryRole
    12. };
    13.  
    14. QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    15. bool setData(const QModelIndex &index, const QVariant &value, int role);
    16. Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    17. QVariant headerData(int section, Qt::Orientation orientation,
    18. int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    19. QModelIndex index(int row, int column,
    20. const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    21. QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
    22. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    23. int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    24.  
    25. protected:
    26. QHash<int, QByteArray> roleNames() const;
    27.  
    28. private:
    29. TreeItem *rootItem;
    30. };
    To copy to clipboard, switch view to plain text mode 

    Here's the code of the treeView
    Qt Code:
    1. TreeView {
    2. id: view
    3. anchors.fill: parent
    4. anchors.margins: 2 * 12 + row.height
    5. model: treeModel
    6. selection: selectionModel
    7.  
    8. headerVisible: false
    9.  
    10. itemDelegate:
    11. Component {
    12. id: dragDelegate
    13.  
    14. MouseArea {
    15. id: dragArea
    16.  
    17. property bool held: false
    18.  
    19. height: content.height
    20.  
    21. drag.target: held ? content : undefined
    22. drag.axis: Drag.YAxis
    23.  
    24. onPressAndHold: held = true
    25. onReleased: held = false
    26.  
    27. Rectangle {
    28. id: content
    29. anchors {
    30. horizontalCenter: parent.horizontalCenter
    31. verticalCenter: parent.verticalCenter
    32. }
    33. width: dragArea.width
    34. height: 20
    35. Text{
    36. anchors.verticalCenter: parent.verticalCenter
    37. text: styleData.value
    38. }
    39.  
    40. border.width: 1
    41. border.color: "lightsteelblue"
    42. color: dragArea.held ?
    43. "lightsteelblue" : "green"
    44. Behavior on color { ColorAnimation { duration: 100 } }
    45. radius: 2
    46. states: State {
    47. when: dragArea.held
    48.  
    49. ParentChange { target: content; parent: root }
    50. AnchorChanges {
    51. target: content
    52. anchors { horizontalCenter: undefined; verticalCenter: undefined }
    53. }
    54. }
    55. }
    56. }
    57. }
    58.  
    59. TableViewColumn {
    60. title: "Name"
    61. role: "Title"
    62. resizable: true
    63.  
    64. }
    65.  
    66. onClicked: {
    67. console.log("clicked", index)
    68. }
    69.  
    70. onDoubleClicked: {
    71. console.log("double clicked " + index)
    72. clickedIndex = index
    73. console.log("DoubleClickedIndex " + clickedIndex)
    74. textEditRect.visible = true;
    75. textEdit.text = fileSystemModel.data(index, "Title")
    76. textEditRect.forceActiveFocus()
    77. isExpanded(index) ? collapse(index) : expand(index)
    78. }
    79. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Moving items in QAbstractItemModel
    By arturo182 in forum Qt Programming
    Replies: 13
    Last Post: 1st June 2011, 01:53
  2. How to switch view in sliding/moving new view from right of device to the left
    By curiouswalker in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th November 2010, 12:55
  3. Moving Items with itemChange?
    By konvex in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 15:36
  4. QAbstractItemModel bold items
    By SiLiZiUMM in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2008, 20:48
  5. Problem inserting child items into a QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2006, 19:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.