summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews/editabletreemodel
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/itemviews/editabletreemodel')
-rw-r--r--examples/widgets/itemviews/editabletreemodel/default.txt40
-rw-r--r--examples/widgets/itemviews/editabletreemodel/editabletreemodel.desktop11
-rw-r--r--examples/widgets/itemviews/editabletreemodel/editabletreemodel.pro18
-rw-r--r--examples/widgets/itemviews/editabletreemodel/editabletreemodel.qrc5
-rw-r--r--examples/widgets/itemviews/editabletreemodel/main.cpp53
-rw-r--r--examples/widgets/itemviews/editabletreemodel/mainwindow.cpp181
-rw-r--r--examples/widgets/itemviews/editabletreemodel/mainwindow.h71
-rw-r--r--examples/widgets/itemviews/editabletreemodel/mainwindow.ui128
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treeitem.cpp179
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treeitem.h74
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treemodel.cpp288
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treemodel.h97
12 files changed, 1145 insertions, 0 deletions
diff --git a/examples/widgets/itemviews/editabletreemodel/default.txt b/examples/widgets/itemviews/editabletreemodel/default.txt
new file mode 100644
index 0000000000..2b2fb579ae
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/default.txt
@@ -0,0 +1,40 @@
+Getting Started How to familiarize yourself with Qt Designer
+ Launching Designer Running the Qt Designer application
+ The User Interface How to interact with Qt Designer
+
+Designing a Component Creating a GUI for your application
+ Creating a Dialog How to create a dialog
+ Composing the Dialog Putting widgets into the dialog example
+ Creating a Layout Arranging widgets on a form
+ Signal and Slot Connections Making widget communicate with each other
+
+Using a Component in Your Application Generating code from forms
+ The Direct Approach Using a form without any adjustments
+ The Single Inheritance Approach Subclassing a form's base class
+ The Multiple Inheritance Approach Subclassing the form itself
+ Automatic Connections Connecting widgets using a naming scheme
+ A Dialog Without Auto-Connect How to connect widgets without a naming scheme
+ A Dialog With Auto-Connect Using automatic connections
+
+Form Editing Mode How to edit a form in Qt Designer
+ Managing Forms Loading and saving forms
+ Editing a Form Basic editing techniques
+ The Property Editor Changing widget properties
+ The Object Inspector Examining the hierarchy of objects on a form
+ Layouts Objects that arrange widgets on a form
+ Applying and Breaking Layouts Managing widgets in layouts
+ Horizontal and Vertical Layouts Standard row and column layouts
+ The Grid Layout Arranging widgets in a matrix
+ Previewing Forms Checking that the design works
+
+Using Containers How to group widgets together
+ General Features Common container features
+ Frames QFrame
+ Group Boxes QGroupBox
+ Stacked Widgets QStackedWidget
+ Tab Widgets QTabWidget
+ Toolbox Widgets QToolBox
+
+Connection Editing Mode Connecting widgets together with signals and slots
+ Connecting Objects Making connections in Qt Designer
+ Editing Connections Changing existing connections
diff --git a/examples/widgets/itemviews/editabletreemodel/editabletreemodel.desktop b/examples/widgets/itemviews/editabletreemodel/editabletreemodel.desktop
new file mode 100644
index 0000000000..17d17330df
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/editabletreemodel.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Encoding=UTF-8
+Version=1.0
+Type=Application
+Terminal=false
+Name=Editable Tree Model
+Exec=/opt/usr/bin/editabletreemodel
+Icon=editabletreemodel
+X-Window-Icon=
+X-HildonDesk-ShowInToolbar=true
+X-Osso-Type=application/x-executable
diff --git a/examples/widgets/itemviews/editabletreemodel/editabletreemodel.pro b/examples/widgets/itemviews/editabletreemodel/editabletreemodel.pro
new file mode 100644
index 0000000000..f86298542d
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/editabletreemodel.pro
@@ -0,0 +1,18 @@
+FORMS = mainwindow.ui
+HEADERS = mainwindow.h \
+ treeitem.h \
+ treemodel.h
+RESOURCES = editabletreemodel.qrc
+SOURCES = mainwindow.cpp \
+ treeitem.cpp \
+ treemodel.cpp \
+ main.cpp
+CONFIG += qt
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/itemviews/editabletreemodel
+sources.files = $$FORMS $$HEADERS $$RESOURCES $$SOURCES *.pro *.txt
+sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/itemviews/editabletreemodel
+INSTALLS += target sources
+
+QT += widgets
diff --git a/examples/widgets/itemviews/editabletreemodel/editabletreemodel.qrc b/examples/widgets/itemviews/editabletreemodel/editabletreemodel.qrc
new file mode 100644
index 0000000000..2fb48f2e62
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/editabletreemodel.qrc
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource prefix="/" >
+ <file>default.txt</file>
+ </qresource>
+</RCC>
diff --git a/examples/widgets/itemviews/editabletreemodel/main.cpp b/examples/widgets/itemviews/editabletreemodel/main.cpp
new file mode 100644
index 0000000000..516adf1543
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/main.cpp
@@ -0,0 +1,53 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+
+#include "mainwindow.h"
+
+int main(int argc, char *argv[])
+{
+ Q_INIT_RESOURCE(editabletreemodel);
+
+ QApplication app(argc, argv);
+ MainWindow window;
+ window.show();
+ return app.exec();
+}
diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
new file mode 100644
index 0000000000..115b02e28e
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
@@ -0,0 +1,181 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+
+#include "mainwindow.h"
+#include "treemodel.h"
+
+MainWindow::MainWindow(QWidget *parent)
+ : QMainWindow(parent)
+{
+ setupUi(this);
+
+
+ QStringList headers;
+ headers << tr("Title") << tr("Description");
+
+ QFile file(":/default.txt");
+ file.open(QIODevice::ReadOnly);
+ TreeModel *model = new TreeModel(headers, file.readAll());
+ file.close();
+
+ view->setModel(model);
+ for (int column = 0; column < model->columnCount(); ++column)
+ view->resizeColumnToContents(column);
+
+ connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
+
+ connect(view->selectionModel(),
+ SIGNAL(selectionChanged(const QItemSelection &,
+ const QItemSelection &)),
+ this, SLOT(updateActions()));
+
+ connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions()));
+ connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow()));
+ connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn()));
+ connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow()));
+ connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn()));
+ connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild()));
+
+ updateActions();
+}
+
+void MainWindow::insertChild()
+{
+ QModelIndex index = view->selectionModel()->currentIndex();
+ QAbstractItemModel *model = view->model();
+
+ if (model->columnCount(index) == 0) {
+ if (!model->insertColumn(0, index))
+ return;
+ }
+
+ if (!model->insertRow(0, index))
+ return;
+
+ for (int column = 0; column < model->columnCount(index); ++column) {
+ QModelIndex child = model->index(0, column, index);
+ model->setData(child, QVariant("[No data]"), Qt::EditRole);
+ if (!model->headerData(column, Qt::Horizontal).isValid())
+ model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"),
+ Qt::EditRole);
+ }
+
+ view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
+ QItemSelectionModel::ClearAndSelect);
+ updateActions();
+}
+
+bool MainWindow::insertColumn(const QModelIndex &parent)
+{
+ QAbstractItemModel *model = view->model();
+ int column = view->selectionModel()->currentIndex().column();
+
+ // Insert a column in the parent item.
+ bool changed = model->insertColumn(column + 1, parent);
+ if (changed)
+ model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
+ Qt::EditRole);
+
+ updateActions();
+
+ return changed;
+}
+
+void MainWindow::insertRow()
+{
+ QModelIndex index = view->selectionModel()->currentIndex();
+ QAbstractItemModel *model = view->model();
+
+ if (!model->insertRow(index.row()+1, index.parent()))
+ return;
+
+ updateActions();
+
+ for (int column = 0; column < model->columnCount(index.parent()); ++column) {
+ QModelIndex child = model->index(index.row()+1, column, index.parent());
+ model->setData(child, QVariant("[No data]"), Qt::EditRole);
+ }
+}
+
+bool MainWindow::removeColumn(const QModelIndex &parent)
+{
+ QAbstractItemModel *model = view->model();
+ int column = view->selectionModel()->currentIndex().column();
+
+ // Insert columns in each child of the parent item.
+ bool changed = model->removeColumn(column, parent);
+
+ if (!parent.isValid() && changed)
+ updateActions();
+
+ return changed;
+}
+
+void MainWindow::removeRow()
+{
+ QModelIndex index = view->selectionModel()->currentIndex();
+ QAbstractItemModel *model = view->model();
+ if (model->removeRow(index.row(), index.parent()))
+ updateActions();
+}
+
+void MainWindow::updateActions()
+{
+ bool hasSelection = !view->selectionModel()->selection().isEmpty();
+ removeRowAction->setEnabled(hasSelection);
+ removeColumnAction->setEnabled(hasSelection);
+
+ bool hasCurrent = view->selectionModel()->currentIndex().isValid();
+ insertRowAction->setEnabled(hasCurrent);
+ insertColumnAction->setEnabled(hasCurrent);
+
+ if (hasCurrent) {
+ view->closePersistentEditor(view->selectionModel()->currentIndex());
+
+ int row = view->selectionModel()->currentIndex().row();
+ int column = view->selectionModel()->currentIndex().column();
+ if (view->selectionModel()->currentIndex().parent().isValid())
+ statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
+ else
+ statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
+ }
+}
diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.h b/examples/widgets/itemviews/editabletreemodel/mainwindow.h
new file mode 100644
index 0000000000..e45037c2e0
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include <QModelIndex>
+
+#include "ui_mainwindow.h"
+
+class QAction;
+class QTreeView;
+class QWidget;
+
+class MainWindow : public QMainWindow, private Ui::MainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow(QWidget *parent = 0);
+
+public slots:
+ void updateActions();
+
+private slots:
+ void insertChild();
+ bool insertColumn(const QModelIndex &parent = QModelIndex());
+ void insertRow();
+ bool removeColumn(const QModelIndex &parent = QModelIndex());
+ void removeRow();
+};
+
+#endif
diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.ui b/examples/widgets/itemviews/editabletreemodel/mainwindow.ui
new file mode 100644
index 0000000000..2ad084ae24
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.ui
@@ -0,0 +1,128 @@
+<ui version="4.0" >
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>573</width>
+ <height>468</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Editable Tree Model</string>
+ </property>
+ <widget class="QWidget" name="centralwidget" >
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>0</number>
+ </property>
+ <item>
+ <widget class="QTreeView" name="view" >
+ <property name="alternatingRowColors" >
+ <bool>true</bool>
+ </property>
+ <property name="selectionBehavior" >
+ <enum>QAbstractItemView::SelectItems</enum>
+ </property>
+ <property name="horizontalScrollMode" >
+ <enum>QAbstractItemView::ScrollPerPixel</enum>
+ </property>
+ <property name="animated" >
+ <bool>false</bool>
+ </property>
+ <property name="allColumnsShowFocus" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QMenuBar" name="menubar" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>573</width>
+ <height>31</height>
+ </rect>
+ </property>
+ <widget class="QMenu" name="fileMenu" >
+ <property name="title" >
+ <string>&amp;File</string>
+ </property>
+ <addaction name="exitAction" />
+ </widget>
+ <widget class="QMenu" name="actionsMenu" >
+ <property name="title" >
+ <string>&amp;Actions</string>
+ </property>
+ <addaction name="insertRowAction" />
+ <addaction name="insertColumnAction" />
+ <addaction name="separator" />
+ <addaction name="removeRowAction" />
+ <addaction name="removeColumnAction" />
+ <addaction name="separator" />
+ <addaction name="insertChildAction" />
+ </widget>
+ <addaction name="fileMenu" />
+ <addaction name="actionsMenu" />
+ </widget>
+ <widget class="QStatusBar" name="statusbar" />
+ <action name="exitAction" >
+ <property name="text" >
+ <string>E&amp;xit</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+Q</string>
+ </property>
+ </action>
+ <action name="insertRowAction" >
+ <property name="text" >
+ <string>Insert Row</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+I, R</string>
+ </property>
+ </action>
+ <action name="removeRowAction" >
+ <property name="text" >
+ <string>Remove Row</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+R, R</string>
+ </property>
+ </action>
+ <action name="insertColumnAction" >
+ <property name="text" >
+ <string>Insert Column</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+I, C</string>
+ </property>
+ </action>
+ <action name="removeColumnAction" >
+ <property name="text" >
+ <string>Remove Column</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+R, C</string>
+ </property>
+ </action>
+ <action name="insertChildAction" >
+ <property name="text" >
+ <string>Insert Child</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+N</string>
+ </property>
+ </action>
+ </widget>
+ <resources>
+ <include location="editabletreemodel.qrc" />
+ </resources>
+ <connections/>
+</ui>
diff --git a/examples/widgets/itemviews/editabletreemodel/treeitem.cpp b/examples/widgets/itemviews/editabletreemodel/treeitem.cpp
new file mode 100644
index 0000000000..c728f21a92
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/treeitem.cpp
@@ -0,0 +1,179 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*
+ treeitem.cpp
+
+ A container for items of data supplied by the simple tree model.
+*/
+
+#include <QStringList>
+
+#include "treeitem.h"
+
+//! [0]
+TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
+{
+ parentItem = parent;
+ itemData = data;
+}
+//! [0]
+
+//! [1]
+TreeItem::~TreeItem()
+{
+ qDeleteAll(childItems);
+}
+//! [1]
+
+//! [2]
+TreeItem *TreeItem::child(int number)
+{
+ return childItems.value(number);
+}
+//! [2]
+
+//! [3]
+int TreeItem::childCount() const
+{
+ return childItems.count();
+}
+//! [3]
+
+//! [4]
+int TreeItem::childNumber() const
+{
+ if (parentItem)
+ return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
+
+ return 0;
+}
+//! [4]
+
+//! [5]
+int TreeItem::columnCount() const
+{
+ return itemData.count();
+}
+//! [5]
+
+//! [6]
+QVariant TreeItem::data(int column) const
+{
+ return itemData.value(column);
+}
+//! [6]
+
+//! [7]
+bool TreeItem::insertChildren(int position, int count, int columns)
+{
+ if (position < 0 || position > childItems.size())
+ return false;
+
+ for (int row = 0; row < count; ++row) {
+ QVector<QVariant> data(columns);
+ TreeItem *item = new TreeItem(data, this);
+ childItems.insert(position, item);
+ }
+
+ return true;
+}
+//! [7]
+
+//! [8]
+bool TreeItem::insertColumns(int position, int columns)
+{
+ if (position < 0 || position > itemData.size())
+ return false;
+
+ for (int column = 0; column < columns; ++column)
+ itemData.insert(position, QVariant());
+
+ foreach (TreeItem *child, childItems)
+ child->insertColumns(position, columns);
+
+ return true;
+}
+//! [8]
+
+//! [9]
+TreeItem *TreeItem::parent()
+{
+ return parentItem;
+}
+//! [9]
+
+//! [10]
+bool TreeItem::removeChildren(int position, int count)
+{
+ if (position < 0 || position + count > childItems.size())
+ return false;
+
+ for (int row = 0; row < count; ++row)
+ delete childItems.takeAt(position);
+
+ return true;
+}
+//! [10]
+
+bool TreeItem::removeColumns(int position, int columns)
+{
+ if (position < 0 || position + columns > itemData.size())
+ return false;
+
+ for (int column = 0; column < columns; ++column)
+ itemData.remove(position);
+
+ foreach (TreeItem *child, childItems)
+ child->removeColumns(position, columns);
+
+ return true;
+}
+
+//! [11]
+bool TreeItem::setData(int column, const QVariant &value)
+{
+ if (column < 0 || column >= itemData.size())
+ return false;
+
+ itemData[column] = value;
+ return true;
+}
+//! [11]
diff --git a/examples/widgets/itemviews/editabletreemodel/treeitem.h b/examples/widgets/itemviews/editabletreemodel/treeitem.h
new file mode 100644
index 0000000000..8426514759
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/treeitem.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef TREEITEM_H
+#define TREEITEM_H
+
+#include <QList>
+#include <QVariant>
+#include <QVector>
+
+//! [0]
+class TreeItem
+{
+public:
+ TreeItem(const QVector<QVariant> &data, TreeItem *parent = 0);
+ ~TreeItem();
+
+ TreeItem *child(int number);
+ int childCount() const;
+ int columnCount() const;
+ QVariant data(int column) const;
+ bool insertChildren(int position, int count, int columns);
+ bool insertColumns(int position, int columns);
+ TreeItem *parent();
+ bool removeChildren(int position, int count);
+ bool removeColumns(int position, int columns);
+ int childNumber() const;
+ bool setData(int column, const QVariant &value);
+
+private:
+ QList<TreeItem*> childItems;
+ QVector<QVariant> itemData;
+ TreeItem *parentItem;
+};
+//! [0]
+
+#endif
diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
new file mode 100644
index 0000000000..8192c14e9f
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
@@ -0,0 +1,288 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+
+#include "treeitem.h"
+#include "treemodel.h"
+
+//! [0]
+TreeModel::TreeModel(const QStringList &headers, const QString &data,
+ QObject *parent)
+ : QAbstractItemModel(parent)
+{
+ QVector<QVariant> rootData;
+ foreach (QString header, headers)
+ rootData << header;
+
+ rootItem = new TreeItem(rootData);
+ setupModelData(data.split(QString("\n")), rootItem);
+}
+//! [0]
+
+//! [1]
+TreeModel::~TreeModel()
+{
+ delete rootItem;
+}
+//! [1]
+
+//! [2]
+int TreeModel::columnCount(const QModelIndex & /* parent */) const
+{
+ return rootItem->columnCount();
+}
+//! [2]
+
+QVariant TreeModel::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (role != Qt::DisplayRole && role != Qt::EditRole)
+ return QVariant();
+
+ TreeItem *item = getItem(index);
+
+ return item->data(index.column());
+}
+
+//! [3]
+Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return 0;
+
+ return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+//! [3]
+
+//! [4]
+TreeItem *TreeModel::getItem(const QModelIndex &index) const
+{
+ if (index.isValid()) {
+ TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
+ if (item) return item;
+ }
+ return rootItem;
+}
+//! [4]
+
+QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
+ int role) const
+{
+ if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+ return rootItem->data(section);
+
+ return QVariant();
+}
+
+//! [5]
+QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
+{
+ if (parent.isValid() && parent.column() != 0)
+ return QModelIndex();
+//! [5]
+
+//! [6]
+ TreeItem *parentItem = getItem(parent);
+
+ TreeItem *childItem = parentItem->child(row);
+ if (childItem)
+ return createIndex(row, column, childItem);
+ else
+ return QModelIndex();
+}
+//! [6]
+
+bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
+{
+ bool success;
+
+ beginInsertColumns(parent, position, position + columns - 1);
+ success = rootItem->insertColumns(position, columns);
+ endInsertColumns();
+
+ return success;
+}
+
+bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
+{
+ TreeItem *parentItem = getItem(parent);
+ bool success;
+
+ beginInsertRows(parent, position, position + rows - 1);
+ success = parentItem->insertChildren(position, rows, rootItem->columnCount());
+ endInsertRows();
+
+ return success;
+}
+
+//! [7]
+QModelIndex TreeModel::parent(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return QModelIndex();
+
+ TreeItem *childItem = getItem(index);
+ TreeItem *parentItem = childItem->parent();
+
+ if (parentItem == rootItem)
+ return QModelIndex();
+
+ return createIndex(parentItem->childNumber(), 0, parentItem);
+}
+//! [7]
+
+bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
+{
+ bool success;
+
+ beginRemoveColumns(parent, position, position + columns - 1);
+ success = rootItem->removeColumns(position, columns);
+ endRemoveColumns();
+
+ if (rootItem->columnCount() == 0)
+ removeRows(0, rowCount());
+
+ return success;
+}
+
+bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
+{
+ TreeItem *parentItem = getItem(parent);
+ bool success = true;
+
+ beginRemoveRows(parent, position, position + rows - 1);
+ success = parentItem->removeChildren(position, rows);
+ endRemoveRows();
+
+ return success;
+}
+
+//! [8]
+int TreeModel::rowCount(const QModelIndex &parent) const
+{
+ TreeItem *parentItem = getItem(parent);
+
+ return parentItem->childCount();
+}
+//! [8]
+
+bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
+ int role)
+{
+ if (role != Qt::EditRole)
+ return false;
+
+ TreeItem *item = getItem(index);
+ bool result = item->setData(index.column(), value);
+
+ if (result)
+ emit dataChanged(index, index);
+
+ return result;
+}
+
+bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
+ const QVariant &value, int role)
+{
+ if (role != Qt::EditRole || orientation != Qt::Horizontal)
+ return false;
+
+ bool result = rootItem->setData(section, value);
+
+ if (result)
+ emit headerDataChanged(orientation, section, section);
+
+ return result;
+}
+
+void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
+{
+ QList<TreeItem*> parents;
+ QList<int> indentations;
+ parents << parent;
+ indentations << 0;
+
+ int number = 0;
+
+ while (number < lines.count()) {
+ int position = 0;
+ while (position < lines[number].length()) {
+ if (lines[number].mid(position, 1) != " ")
+ break;
+ position++;
+ }
+
+ QString lineData = lines[number].mid(position).trimmed();
+
+ if (!lineData.isEmpty()) {
+ // Read the column data from the rest of the line.
+ QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
+ QVector<QVariant> columnData;
+ for (int column = 0; column < columnStrings.count(); ++column)
+ columnData << columnStrings[column];
+
+ if (position > indentations.last()) {
+ // The last child of the current parent is now the new parent
+ // unless the current parent has no children.
+
+ if (parents.last()->childCount() > 0) {
+ parents << parents.last()->child(parents.last()->childCount()-1);
+ indentations << position;
+ }
+ } else {
+ while (position < indentations.last() && parents.count() > 0) {
+ parents.pop_back();
+ indentations.pop_back();
+ }
+ }
+
+ // Append a new item to the current parent's list of children.
+ TreeItem *parent = parents.last();
+ parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
+ for (int column = 0; column < columnData.size(); ++column)
+ parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
+ }
+
+ number++;
+ }
+}
diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.h b/examples/widgets/itemviews/editabletreemodel/treemodel.h
new file mode 100644
index 0000000000..f124df2e1d
--- /dev/null
+++ b/examples/widgets/itemviews/editabletreemodel/treemodel.h
@@ -0,0 +1,97 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef TREEMODEL_H
+#define TREEMODEL_H
+
+#include <QAbstractItemModel>
+#include <QModelIndex>
+#include <QVariant>
+
+class TreeItem;
+
+//! [0]
+class TreeModel : public QAbstractItemModel
+{
+ Q_OBJECT
+
+public:
+ TreeModel(const QStringList &headers, const QString &data,
+ QObject *parent = 0);
+ ~TreeModel();
+//! [0] //! [1]
+
+ QVariant data(const QModelIndex &index, int role) const;
+ QVariant headerData(int section, Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+
+ QModelIndex index(int row, int column,
+ const QModelIndex &parent = QModelIndex()) const;
+ QModelIndex parent(const QModelIndex &index) const;
+
+ int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ int columnCount(const QModelIndex &parent = QModelIndex()) const;
+//! [1]
+
+//! [2]
+ Qt::ItemFlags flags(const QModelIndex &index) const;
+ bool setData(const QModelIndex &index, const QVariant &value,
+ int role = Qt::EditRole);
+ bool setHeaderData(int section, Qt::Orientation orientation,
+ const QVariant &value, int role = Qt::EditRole);
+
+ bool insertColumns(int position, int columns,
+ const QModelIndex &parent = QModelIndex());
+ bool removeColumns(int position, int columns,
+ const QModelIndex &parent = QModelIndex());
+ bool insertRows(int position, int rows,
+ const QModelIndex &parent = QModelIndex());
+ bool removeRows(int position, int rows,
+ const QModelIndex &parent = QModelIndex());
+
+private:
+ void setupModelData(const QStringList &lines, TreeItem *parent);
+ TreeItem *getItem(const QModelIndex &index) const;
+
+ TreeItem *rootItem;
+};
+//! [2]
+
+#endif