-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f3bf077
Showing
11 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.user | ||
bin | ||
build | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) | ||
|
||
project(QTreeView_example) | ||
|
||
find_package(Qt4 REQUIRED) | ||
INCLUDE (${QT_USE_FILE}) | ||
ADD_DEFINITIONS (${QT_DEFINITIONS}) | ||
|
||
# Add bin and build dir to include | ||
set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||
include_directories(${CMAKE_SOURCE_DIR}/src) | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) | ||
set(CMAKE_SHARED_LIBRARY_SUFFIX .so) | ||
|
||
add_subdirectory(src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Data Item | ||
add_library(data_item_widget SHARED QDataItem.cpp ) | ||
target_link_libraries(data_item_widget ${QT_LIBRARIES}) | ||
|
||
# Data item model | ||
QT4_WRAP_CPP(data_item_tree_widget_moc QDataItemTree.h) | ||
add_library(data_item_tree_widget SHARED QDataItemTree.cpp ${data_item_tree_widget_moc}) | ||
target_link_libraries(data_item_tree_widget ${QT_LIBRARIES} data_item_widget) | ||
|
||
# Refine calibration | ||
QT4_WRAP_CPP(mainwidget_moc QMainWidget.h) | ||
QT4_WRAP_UI(mainwidget_ui QMainWidget.ui) | ||
add_library(mainwidget_widget SHARED QMainWidget.cpp ${mainwidget_moc} ${mainwidget_ui} ) | ||
target_link_libraries(mainwidget_widget ${QT_LIBRARIES} data_item_tree_widget) | ||
add_executable(QTreeView_example main.cpp) | ||
target_link_libraries(QTreeView_example ${QT_LIBRARIES} mainwidget_widget ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "QDataItem.h" | ||
|
||
QDataItem::QDataItem(const QList<QVariant> &data, QDataItem *parentItem) | ||
{ | ||
m_text = data[0].toString(); | ||
m_parentItem = parentItem; | ||
m_itemData = data; | ||
} | ||
|
||
QDataItem::~QDataItem() | ||
{ | ||
qDeleteAll(m_childItems); | ||
} | ||
|
||
void QDataItem::appendChild(QDataItem *child) | ||
{ | ||
m_childItems.append(child); | ||
} | ||
|
||
QDataItem* QDataItem::child(int row) | ||
{ | ||
return m_childItems.value(row); | ||
} | ||
|
||
int QDataItem::childCount() const | ||
{ | ||
return m_childItems.count(); | ||
} | ||
|
||
int QDataItem::columnCount() const | ||
{ | ||
return m_itemData.count(); | ||
} | ||
|
||
QVariant QDataItem::data(int column) const | ||
{ | ||
return m_itemData.value(column); | ||
} | ||
|
||
int QDataItem::row() const | ||
{ | ||
if (m_parentItem) | ||
return m_parentItem->m_childItems.indexOf(const_cast<QDataItem*>(this)); | ||
|
||
return 0; | ||
} | ||
|
||
QDataItem* QDataItem::parentItem() | ||
{ | ||
return m_parentItem; | ||
} | ||
|
||
QString QDataItem::getText() | ||
{ | ||
return m_text; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef QDataItem_H | ||
#define QDataItem_H | ||
|
||
#include <QVariant> | ||
|
||
class QDataItem | ||
{ | ||
public: | ||
explicit QDataItem(const QList<QVariant> &data, QDataItem *parentItem = 0); | ||
~QDataItem(); | ||
|
||
void appendChild(QDataItem *child); | ||
|
||
QDataItem *child(int row); | ||
int childCount() const; | ||
int columnCount() const; | ||
QVariant data(int column) const; | ||
int row() const; | ||
QDataItem *parentItem(); | ||
|
||
QString getText(); | ||
private: | ||
QList<QDataItem*> m_childItems; | ||
QList<QVariant> m_itemData; | ||
QDataItem *m_parentItem; | ||
|
||
QString m_text; | ||
}; | ||
|
||
#endif // QDataItem_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include "QDataItemTree.h" | ||
#include <QStringList> | ||
|
||
QDataItemTree::QDataItemTree(const QString &data, QObject *parent): | ||
QAbstractItemModel(parent) | ||
{ | ||
QList<QVariant> rootData; | ||
rootData << data; | ||
rootItem = new QDataItem(rootData); | ||
} | ||
|
||
QDataItemTree::~QDataItemTree() | ||
{ | ||
delete rootItem; | ||
} | ||
|
||
QVariant QDataItemTree::data(const QModelIndex &index, int role) const | ||
{ | ||
if (!index.isValid()) | ||
return QVariant(); | ||
|
||
if (role != Qt::DisplayRole) | ||
return QVariant(); | ||
|
||
QDataItem *item = static_cast<QDataItem*>(index.internalPointer()); | ||
|
||
return item->data(index.column()); | ||
} | ||
|
||
Qt::ItemFlags QDataItemTree::flags(const QModelIndex &index) const | ||
{ | ||
if (!index.isValid()) | ||
return 0; | ||
|
||
return QAbstractItemModel::flags(index); | ||
} | ||
|
||
QVariant QDataItemTree::headerData(int section, Qt::Orientation orientation, | ||
int role) const | ||
{ | ||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) | ||
return rootItem->data(section); | ||
|
||
return QVariant(); | ||
} | ||
|
||
QModelIndex QDataItemTree::index(int row, int column, | ||
const QModelIndex &parent) const | ||
{ | ||
if (!hasIndex(row, column, parent)) | ||
return QModelIndex(); | ||
|
||
QDataItem *parentItem; | ||
|
||
if (!parent.isValid()) | ||
parentItem = rootItem; | ||
else | ||
parentItem = static_cast<QDataItem*>(parent.internalPointer()); | ||
|
||
QDataItem *childItem = parentItem->child(row); | ||
if (childItem) | ||
return createIndex(row, column, childItem); | ||
else | ||
return QModelIndex(); | ||
} | ||
|
||
QModelIndex QDataItemTree::parent(const QModelIndex &index) const | ||
{ | ||
if (!index.isValid()) | ||
return QModelIndex(); | ||
|
||
QDataItem *childItem = static_cast<QDataItem*>(index.internalPointer()); | ||
QDataItem *parentItem = childItem->parentItem(); | ||
|
||
if (parentItem == rootItem) | ||
return QModelIndex(); | ||
|
||
return createIndex(parentItem->row(), 0, parentItem); | ||
} | ||
|
||
int QDataItemTree::rowCount(const QModelIndex &parent) const | ||
{ | ||
QDataItem *parentItem; | ||
if (parent.column() > 0) | ||
return 0; | ||
|
||
if (!parent.isValid()) | ||
parentItem = rootItem; | ||
else | ||
parentItem = static_cast<QDataItem*>(parent.internalPointer()); | ||
|
||
return parentItem->childCount(); | ||
} | ||
|
||
int QDataItemTree::columnCount(const QModelIndex &parent) const | ||
{ | ||
if (parent.isValid()) | ||
return static_cast<QDataItem*>(parent.internalPointer())->columnCount(); | ||
else | ||
return rootItem->columnCount(); | ||
} | ||
|
||
QDataItem* QDataItemTree::addBatch(int number) | ||
{ | ||
QString name = "Batch_"+QString::number(number); | ||
QList<QVariant> d; | ||
d<<name; | ||
QDataItem* batch = new QDataItem(d,rootItem); | ||
rootItem->appendChild(batch); | ||
return batch; | ||
} | ||
|
||
QDataItem* QDataItemTree::addBatchElement(int number, QDataItem* parent) | ||
{ | ||
QString name = "Element_"+QString::number(number); | ||
QList<QVariant> d; | ||
d<<name; | ||
QDataItem* element = new QDataItem(d,parent); | ||
parent->appendChild(element); | ||
return element; | ||
} | ||
|
||
QDataItem* QDataItemTree::addBatchElementChild(int number, QDataItem* parent) | ||
{ | ||
QString name = "Child_"+QString::number(number); | ||
QList<QVariant> d; | ||
d<<name; | ||
QDataItem* element_child = new QDataItem(d,parent); | ||
parent->appendChild(element_child); | ||
return element_child; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef QDataItemTREE_H | ||
#define QDataItemTREE_H | ||
|
||
#include <QAbstractItemModel> | ||
|
||
#include "QDataItem.h" | ||
|
||
class QDataItemTree : public QAbstractItemModel | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit QDataItemTree(const QString& data, QObject *parent = 0); | ||
~QDataItemTree(); | ||
|
||
QVariant data(const QModelIndex &index, int role) const override; | ||
Qt::ItemFlags flags(const QModelIndex &index) const override; | ||
QVariant headerData(int section, Qt::Orientation orientation, | ||
int role = Qt::DisplayRole) const override; | ||
QModelIndex index(int row, int column, | ||
const QModelIndex &parent = QModelIndex()) const override; | ||
QModelIndex parent(const QModelIndex &index) const override; | ||
int rowCount(const QModelIndex &parent = QModelIndex()) const override; | ||
int columnCount(const QModelIndex &parent = QModelIndex()) const override; | ||
|
||
QDataItem* addBatch(int number); | ||
QDataItem* addBatchElement(int number, QDataItem* parent); | ||
QDataItem* addBatchElementChild(int number, QDataItem* parent); | ||
|
||
private: | ||
|
||
QDataItem *rootItem; | ||
|
||
}; | ||
|
||
#endif // QDataItemTREE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "QMainWidget.h" | ||
#include "ui_QMainWidget.h" | ||
|
||
// Qt | ||
#include <QTreeView> | ||
#include <QInputDialog> | ||
|
||
QMainWidget::QMainWidget(QWidget *parent) : | ||
QMainWindow(parent),n_loaded_(0), | ||
ui(new Ui::QMainWidget) | ||
{ | ||
// Set up Ui | ||
ui->setupUi(this); | ||
|
||
// Data tree | ||
tree_view_model_ = new QDataItemTree("Root"); | ||
ui->treeView->setModel(tree_view_model_); | ||
|
||
// Connect load | ||
connect(ui->addElementsLoad_pushButton,SIGNAL(clicked()),this,SLOT(load())); | ||
connect(ui->addElementsLoad2_pushButton,SIGNAL(clicked()),this,SLOT(load2())); | ||
} | ||
|
||
QMainWidget::~QMainWidget() | ||
{ | ||
delete ui; | ||
} | ||
|
||
void QMainWidget::load() | ||
{ | ||
// How many elements to add | ||
int n_elements = QInputDialog::getInt(this,"Elements","How many elemtns?",10); | ||
|
||
// How many childs per element | ||
int n_childs_per_element = QInputDialog::getInt(this,"Elements","How many elemtns?",10); | ||
|
||
// Add batch to tree | ||
QDataItem* batch = tree_view_model_->addBatch(n_loaded_); | ||
for (int i = 0; i < n_elements; i++) | ||
{ | ||
QDataItem* element = tree_view_model_->addBatchElement(i,batch); | ||
for (int j = 0; j <= n_childs_per_element; j++) | ||
{ | ||
tree_view_model_->addBatchElementChild(j,element); | ||
|
||
} | ||
} | ||
n_loaded_++; | ||
ui->treeView->setModel(tree_view_model_); | ||
|
||
} | ||
|
||
void QMainWidget::load2() | ||
{ | ||
// How many elements to add | ||
int n_elements = QInputDialog::getInt(this,"Elements","How many elemtns?",10); | ||
|
||
// How many childs per element | ||
int n_childs_per_element = QInputDialog::getInt(this,"Elements","How many elemtns?",10); | ||
|
||
// Add batch to tree | ||
tree_view_model_ = new QDataItemTree("Root"); | ||
QDataItem* batch = tree_view_model_->addBatch(n_loaded_); | ||
for (int i = 0; i < n_elements; i++) | ||
{ | ||
QDataItem* element = tree_view_model_->addBatchElement(i,batch); | ||
for (int j = 0; j <= n_childs_per_element; j++) | ||
{ | ||
tree_view_model_->addBatchElementChild(j,element); | ||
|
||
} | ||
} | ||
n_loaded_++; | ||
ui->treeView->setModel(tree_view_model_); | ||
|
||
} |
Oops, something went wrong.