All Projects → Daguerreo → QMLTreeView

Daguerreo / QMLTreeView

Licence: MIT license
A TreeView component for QML/QtQuick 2 with a generic TreeModel

Programming Languages

QML
638 projects
C++
36643 projects - #6 most used programming language
QMake
1090 projects

Projects that are alternatives of or similar to QMLTreeView

TreeEdit
Qml TreeEdit with Controls2 (Qml树结构编辑器,使用Controls2实现)
Stars: ✭ 50 (+138.1%)
Mutual labels:  qml, treeview, qtquick-controls2
pardus-store
Pardus Application Store
Stars: ✭ 32 (+52.38%)
Mutual labels:  qml, qtquick-controls2
QtIosCMake
📱 Deploy Qt Application for iOS with a single macro when using CMake.
Stars: ✭ 35 (+66.67%)
Mutual labels:  qml, qtquick-controls2
CatLearnQt
CatLearnQt,提供QWidget,Quick,网络,串口,基础库和示例。案例软件支持样式切换,与国际化。
Stars: ✭ 63 (+200%)
Mutual labels:  qml, qtquick-controls2
maxLibQt
A collection of C++ classes and QtQuick QML components for use with the Qt framework.
Stars: ✭ 18 (-14.29%)
Mutual labels:  qml, qtquick-controls2
sddm-sugar-dark
The sweetest dark theme around for SDDM, the Simple Desktop Display Manager.
Stars: ✭ 72 (+242.86%)
Mutual labels:  qml, qtquick-controls2
terminal
🚀 Terminal
Stars: ✭ 28 (+33.33%)
Mutual labels:  qml, qtquick-controls2
QmlTreeWidget
A TreeView component, implemented by QML, providing convenient interfaces and customizable stylesheet, also available for iOS and Android.
Stars: ✭ 34 (+61.9%)
Mutual labels:  qml, treeview
qtprotobuf
Protobuf generator and bindings for Qt framework
Stars: ✭ 138 (+557.14%)
Mutual labels:  qml, qt-framework
Pagination
a paging widget based on Qt
Stars: ✭ 22 (+4.76%)
Mutual labels:  qml
qtstatusbar
StatusBar for Qt allows setting the status bar color and theme on Android and iOS.
Stars: ✭ 89 (+323.81%)
Mutual labels:  qml
herald
Pre-alpha end-to-end encrypted instant messenger.
Stars: ✭ 23 (+9.52%)
Mutual labels:  qml
Examples Qt
Shows how to use Qt widgets only by programming code (c++17).
Stars: ✭ 38 (+80.95%)
Mutual labels:  qml
python-qt-live-coding
Live coding environment for Python, Qt and QML.
Stars: ✭ 35 (+66.67%)
Mutual labels:  qml
EEIoT
QML IoT components, for fast and super user friendly IoT projects
Stars: ✭ 38 (+80.95%)
Mutual labels:  qml
ADM-treeView
Pure AngularJs TreeView
Stars: ✭ 30 (+42.86%)
Mutual labels:  treeview
Qt3D-learn
Qt3D-learn
Stars: ✭ 36 (+71.43%)
Mutual labels:  qml
react-folder-tree
A versatile react treeview library that supports custom icons and event handlers
Stars: ✭ 56 (+166.67%)
Mutual labels:  treeview
widgetci
a Cross-Platform Widget Management App. (Win/Linux/Mac)
Stars: ✭ 36 (+71.43%)
Mutual labels:  qml
status-desktop
Status Desktop client made in Nim & QML
Stars: ✭ 163 (+676.19%)
Mutual labels:  qml

QMLTreeView 1.0

This project provides a QML Tree View with a C++ Tree Model for Qt 5.15 and QuickControls 2.

Features

  • QML Tree View
  • C++ Tree Model
  • Item selection
  • Item hovering
  • Customizable layout (color, font, positioning)
  • Delegates to fully customize part of the TreeView
  • A Json Tree View implementation

Usage

C++

int main()
{
   auto america = new TreeItem("America");
   auto asia = new TreeItem("Asia");
   auto europe = new TreeItem("Europe");  
   auto brazil = new TreeItem("Brazil");
   auto canada = new TreeItem("Canada");
   auto italy = new TreeItem("Italy");
   auto portugal = new TreeItem("Portugal");

   auto treeModel = new TreeModel();
   model.addTopLevelItem(america);
   model.addTopLevelItem(asia);
   model.addTopLevelItem(europe);
   model.addItem(america, brazil);
   model.addItem(america, canada);
   model.addItem(europe, italy);
   model.addItem(europe, portugal);

   QQmlApplicationEngine engine;
   engine.rootContext()->setContextProperty("treeModel", treeModel);
   engine.load(url);

   return app.exec();
}

QML

Expose and populate the TreeModel from C++, then just initialize it

    TreeView {
        anchors.fill: parent
        model: treeModel
    }

and this is the result

Platforms

Code has been tested in the following OS:

  • Windows
  • Linux

Installation

  • Copy the modules folder to your project, then add the qmltreeview.pri file or manually add headers, source and resource file.
  • Add the modules path to the QML_IMPORT_PATH.
  • In the main file add the import path to the engine with engine.addImportPath("qrc:/modules");
  • In the QML file you can include the module with import QMLTreeView 1.0

Customization

Selection / Hovering

Hovering and selection can be activated enabling the respective properties

    TreeView {
        anchors.fill: parent
        model: treeModel
        
        selectionEnabled: true
        hoverEnabled: true
    }

Styling

TreeView support color styling for each part. Positioning can be customized with the properties rowHeight, rowPadding and rowSpacing.

    TreeView {
        anchors.fill: parent
        model: treeModel

        selectionEnabled: true
        hoverEnabled: true

        color: "steelblue"
        handleColor: "steelblue"
        hoverColor: "skyblue"
        selectedColor: "cornflowerblue"
        selectedItemColor: "white"
        
        handleStyle: TreeView.Handle.Chevron
        rowHeight: 30
        rowPadding: 30 
        font.pixelSize: 24
    }

Font can be customized through properties font and fontMetrics.

The default handle comes with differents default styles:

    enum Handle {
        Triangle,
        TriangleSmall,
        TriangleOutline,
        TriangleSmallOutline,
        Chevron,
        Arrow
    }

Custom Delegates

These component are provided and can be customized with delegates:

  • contentItem: the control element of each row
  • handle: element to collapse/expand the TreeView
  • highlight: the highlight element

Each delegate can use the property currentRow to retreive the information for its own row. currentRow possess the following properties:

  • currentIndex
  • currentData
  • currentItem
  • itemChildCount
  • expanded
  • selected
  • hasChildren
  • isSelectedIndex
  • isHoveredIndex
  • isSelectedAndHoveredIndex

currentRow.toggle() toggles the expansion of the current section.

TreeView {
    id: delegateTreeView
    anchors.fill: parent
    anchors.margins: 1

    model: treeModel
    selectionEnabled: true

    contentItem: Row {
        spacing: 10

        Rectangle {
            width: parent.height * 0.6
            height: width
            radius: width
            y: width / 3
            color: currentRow.hasChildren ? "tomato" : "lightcoral"
        }

        Text {
            verticalAlignment: Text.AlignVCenter

            color: currentRow.isSelectedIndex ? delegateTreeView.selectedItemColor : delegateTreeView.color
            text: currentRow.currentData
            font: delegateTreeView.font
        }
    }

    handle: Item {
        width: 20
        height: 20
        Rectangle {
            anchors.centerIn: parent
            width: 10
            height: 2
            color: "black"
            visible: currentRow.hasChildren

            Rectangle {
                anchors.centerIn: parent
                width: parent.height
                height: parent.width
                color: parent.color
                visible: parent.visible && !currentRow.expanded
            }
        }
    }

    highlight: Item {
        Rectangle {
            color: "pink"
            width: parent.width * 0.9
            height: parent.height
            anchors.left: parent.left
            radius: 20
        }
        Rectangle {
            color: "pink"
            width: parent.width * 0.2
            height: parent.height
            anchors.right: parent.right
            radius: 20
        }
        
        Behavior on y { NumberAnimation { duration: 150 }}
    }
}

Data Access

Data access for row element is provided by the properties:

  • currentIndex: return the model index for the selected element
  • currentData: return the data for the selected element
  • currentItem: return the visual item of the selected element

Node Manipulation

TreeModel provides the methods addTopLevelItem, addItem, removeItem and clear to add and remove node to the tree. setData instead provides a way to change the data of a given node. Take a look at the Manipulator Example which show a way to manipulate the tree directly from QML.

Customizing Models

Since the tree item store a QVariant as data, it's possible to register your custom type with Q_DECLARE_METATYPE and use the TreeModel as is without modifying it.

JsonTreeView example show how to achieve that, declaring a JsonEntry class and providing it to the model.

{
   "firstName": "John",
   "lastName": "Smith",
   "age": 25,
   "address": {
      "streetAddress": "21 2nd Street",
      "city": "New York",
      "state": "NY",
      "postalCode": "10021",
      "owner": true
   },
   "phoneNumber": [
      {
        "type": "home",
        "number": "212 555-1234"
      },
      {
        "type": "fax",
        "number": "646 555-4567"
      }
   ]
}

main.qml

TreeView {
   id: jsonView
   
   anchors.fill: parent
   anchors.margins: 1
   
   model: jsonModel
   rowPadding: 20
   selectionEnabled: true
   
   contentItem: RowLayout {
      Text {
         verticalAlignment: Text.AlignVCenter
         horizontalAlignment: Text.AlignLeft
         text: currentRow.currentData.key
      }
      
      Text {
         Layout.fillWidth: true
         Layout.rightMargin: 10
         
         verticalAlignment: Text.AlignVCenter
         horizontalAlignment: Text.AlignRight
         text: currentRow.currentData.value ? currentRow.currentData.value : ""
      }
   }
}

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].