void ParseNode::addChild(ParseNode *child)
{
if(child && children.indexOf(child) == 0); // <--- semicolon here?
children.push_back(child);
}
void ParseNode::addChild(ParseNode *child)
{
if(child && children.indexOf(child) == 0); // <--- semicolon here?
children.push_back(child);
}
To copy to clipboard, switch view to plain text mode
void ParseNode::addChild(ParseNode *child)
{
if(child && children.indexOf(child) < 0) {
children.push_back(child);
child->myParent = this;
}
}
void ParseNode::addChild(ParseNode *child)
{
if(child && children.indexOf(child) < 0) {
children.push_back(child);
child->myParent = this;
}
}
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include <QAbstractItemModel>
#include <QTreeView>
template<typename T> class Node {
public:
Node() { parentNode = 0; }
~Node() {
if(parentNode)
parentNode->removeChild(this);
qDeleteAll(children);
}
void addChild(Node *n) {
if(n->parentNode) {
if(n->parentNode == this) return;
n->parentNode->removeChild(n);
n->parentNode = 0;
}
n->parentNode = this;
children.append(n);
}
void removeChild(Node *n) {
if(n->parentNode != this) return;
n->parentNode = 0;
children.removeOne(n);
}
int indexOf(Node *n) const { return children.indexOf(n); }
Node *at(int idx) const { return children.at(idx); }
int count() const { return children.count(); }
Node *parent() const { return parentNode; }
T data;
private:
Node<T> *parentNode;
QList<Node<T>*> children;
};
public:
root = new Node<T>();
}
~TreeModel() { delete root; }
return nodeFromIndex(parent)->count();
}
return ColCount;
}
Node<T> *parentNode = nodeFromIndex(parent);
if(row <
0 || row >
= parentNode
->count
() || column <
0 || column >
= ColCount
) return QModelIndex();
return createIndex(row, column, parentNode->at(row));
}
Node<T> *childNode = nodeFromIndex(child);
Node<T> *parentNode = childNode->parent();
Node<T> *grandparentNode = parentNode->parent();
return createIndex(grandparentNode->indexOf(parentNode), 0, parentNode);
}
qWarning("Reimplement in subclass");
}
protected:
if(idx.isValid()) return static_cast<Node<T>*>(idx.internalPointer());
return root;
}
Node<T> *root;
};
class Model : public TreeModel<int, 1> {
public:
Model
(QObject *parent
= 0) : TreeModel
(parent
) {} if(index.
column() != 0) return QVariant();
if(role
!= Qt
::DisplayRole && role
!= Qt
::EditRole) return QVariant();
Node<int> *n = nodeFromIndex(index);
return n->data;
}
if(index.column() != 0) return false;
if(role != Qt::DisplayRole && role != Qt::EditRole) return false;
Node<int> *n = nodeFromIndex(index);
if(n->data != value.toInt()) {
n->data = value.toInt();
emit dataChanged(index, index);
return true;
}
return true;
}
void addItem(int dat) {
beginInsertRows
(QModelIndex(), root
->count
(), root
->count
());
Node<int> *n = new Node<int>();
n->data = dat;
Node<int> *sn = new Node<int>();
sn->data = dat*10;
n->addChild(sn);
root->addChild(n);
endInsertRows();
}
};
public:
Adder(Model *model, int interval = 2000) {
m_model = model;
startTimer(interval);
}
protected:
int val = rand() % 100;
m_model->addItem(val);
}
private:
Model *m_model;
};
int main(int argc, char *argv[])
{
Model model;
// TreeModel<int,1> model;
w.setModel(&model);
w.show();
Adder adder(&model, 1000);
return a.exec();
}
#include <QApplication>
#include <QAbstractItemModel>
#include <QTreeView>
template<typename T> class Node {
public:
Node() { parentNode = 0; }
~Node() {
if(parentNode)
parentNode->removeChild(this);
qDeleteAll(children);
}
void addChild(Node *n) {
if(n->parentNode) {
if(n->parentNode == this) return;
n->parentNode->removeChild(n);
n->parentNode = 0;
}
n->parentNode = this;
children.append(n);
}
void removeChild(Node *n) {
if(n->parentNode != this) return;
n->parentNode = 0;
children.removeOne(n);
}
int indexOf(Node *n) const { return children.indexOf(n); }
Node *at(int idx) const { return children.at(idx); }
int count() const { return children.count(); }
Node *parent() const { return parentNode; }
T data;
private:
Node<T> *parentNode;
QList<Node<T>*> children;
};
template<typename T, int ColCount> class TreeModel : public QAbstractItemModel {
public:
TreeModel(QObject *parent = 0) : QAbstractItemModel(parent) {
root = new Node<T>();
}
~TreeModel() { delete root; }
int rowCount(const QModelIndex &parent = QModelIndex()) const {
return nodeFromIndex(parent)->count();
}
int columnCount(const QModelIndex &parent = QModelIndex()) const {
return ColCount;
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
Node<T> *parentNode = nodeFromIndex(parent);
if(row < 0 || row >= parentNode->count() || column < 0 || column >= ColCount) return QModelIndex();
return createIndex(row, column, parentNode->at(row));
}
QModelIndex parent(const QModelIndex &child) const {
Node<T> *childNode = nodeFromIndex(child);
Node<T> *parentNode = childNode->parent();
if(parentNode == root) return QModelIndex();
Node<T> *grandparentNode = parentNode->parent();
return createIndex(grandparentNode->indexOf(parentNode), 0, parentNode);
}
QVariant data(const QModelIndex &index, int role) const {
qWarning("Reimplement in subclass");
return QVariant();
}
protected:
Node<T>* nodeFromIndex(const QModelIndex &idx) const {
if(idx.isValid()) return static_cast<Node<T>*>(idx.internalPointer());
return root;
}
Node<T> *root;
};
class Model : public TreeModel<int, 1> {
public:
Model(QObject *parent = 0) : TreeModel(parent) {}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
if(index.column() != 0) return QVariant();
if(role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
Node<int> *n = nodeFromIndex(index);
return n->data;
}
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) {
if(index.column() != 0) return false;
if(role != Qt::DisplayRole && role != Qt::EditRole) return false;
Node<int> *n = nodeFromIndex(index);
if(n->data != value.toInt()) {
n->data = value.toInt();
emit dataChanged(index, index);
return true;
}
return true;
}
void addItem(int dat) {
beginInsertRows(QModelIndex(), root->count(), root->count());
Node<int> *n = new Node<int>();
n->data = dat;
Node<int> *sn = new Node<int>();
sn->data = dat*10;
n->addChild(sn);
root->addChild(n);
endInsertRows();
}
};
class Adder : public QObject {
public:
Adder(Model *model, int interval = 2000) {
m_model = model;
startTimer(interval);
}
protected:
void timerEvent(QTimerEvent *) {
int val = rand() % 100;
m_model->addItem(val);
}
private:
Model *m_model;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView w;
Model model;
// TreeModel<int,1> model;
w.setModel(&model);
w.show();
Adder adder(&model, 1000);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks