travlr
6th June 2009, 19:28
Hi all.
My class, 'H5Model', is a QAbstractItemModel subclass.
It is resized, on demand, when the QTreeView item is clicked.
The class built fine until I added 'insertRows(...)' functionality using 'beginInsertRows(...)' and 'endInsertRows()' in the implementation, I get 2 build errors saying:
'beginInsertRows' was not declared in this scope. and
'endInsertRows' was not declared in this scope.
The declaration and implementation are below.
'H5Node::loadChildren()' is called twice, first time returns the number of children, second time actually appends the children to the data structure. (H5Node is the item data structure)
I'm using Qt-4.5 last updated from the git repository on June 3, 2009.
I'm using Qt-Creator and gcc when building.
The build directory has been cleaned completely and qmake re-run before attempting to build.
The "editabletreemodel" Qt example, which is similar, builds without any trouble.
So, given the situtation as it is stated above, I am baffled. :crying:
I certainly appreciate your help.
~travlr :)
h5model.h
#ifndef H5MODEL_H
#define H5MODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class H5Node;
class H5Model : public QAbstractItemModel
{
Q_OBJECT
public:
H5Model(QObject* parent=0);
~H5Model();
// readonly
QVariant data(const QModelIndex & index, int role) const;
Qt::ItemFlags flags(const QModelIndex & index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex & parent) const;
QModelIndex parent(const QModelIndex & index) const;
int rowCount(const QModelIndex & parent=QModelIndex()) const;
int columnCount(const QModelIndex & parent=QModelIndex()) const;
// resizeable
bool insertRows(int row, int count, const QModelIndex & parent=QModelIndex());
public slots:
void h5TreeClicked(const QModelIndex & index);
private:
// variables
H5Node* m_rootNode;
};
#endif // H5MODEL_H
h5model.cpp
#include <QtGui>
#include <QDebug>
#include "h5node.h"
#include "h5model.h"
H5Model::H5Model(QObject* parent) : QAbstractItemModel(parent)
{
m_rootNode = new H5Node(H5Node::Root, "/data/data", 0);
m_rootNode->loadChildren();
}
H5Model::~H5Model()
{
delete m_rootNode;
}
int H5Model::columnCount(const QModelIndex & parent) const
{
if (parent.isValid())
return static_cast<H5Node*>(parent.internalPointer())->columnCount();
else
return m_rootNode->columnCount();
}
QVariant H5Model::data(const QModelIndex & index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
H5Node* node = static_cast<H5Node*>(index.internalPointer());
return node->data(index.column());
}
Qt::ItemFlags H5Model::flags(const QModelIndex & index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant H5Model::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0)
return "Name";
else if (section == 1)
return "Type";
else
return QVariant();
}
return QVariant();
}
QModelIndex H5Model::index(int row, int column, const QModelIndex & parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
H5Node* parentItem;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
H5Node* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex H5Model::parent(const QModelIndex & index) const
{
if (!index.isValid())
return QModelIndex();
H5Node* childItem = static_cast<H5Node*>(index.internalPointer());
H5Node* parentItem = childItem->parent();
if (parentItem == m_rootNode)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int H5Model::rowCount(const QModelIndex & parent) const
{
H5Node* parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
return parentItem->childCount();
}
bool insertRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(row)
Q_UNUSED(count)
H5Node* node = static_cast<H5Node*>(parent.internalPointer());
int numChildren = node->loadChildren();
if (numChildren == 0) {
return false;
}
beginInsertRows(parent, 0, numChildren - 1);
node->loadChildren();
endInsertRows();
return true;
}
// slot
void H5Model::h5TreeClicked(const QModelIndex & index)
{
insertRows(0, 0, index);
}
My class, 'H5Model', is a QAbstractItemModel subclass.
It is resized, on demand, when the QTreeView item is clicked.
The class built fine until I added 'insertRows(...)' functionality using 'beginInsertRows(...)' and 'endInsertRows()' in the implementation, I get 2 build errors saying:
'beginInsertRows' was not declared in this scope. and
'endInsertRows' was not declared in this scope.
The declaration and implementation are below.
'H5Node::loadChildren()' is called twice, first time returns the number of children, second time actually appends the children to the data structure. (H5Node is the item data structure)
I'm using Qt-4.5 last updated from the git repository on June 3, 2009.
I'm using Qt-Creator and gcc when building.
The build directory has been cleaned completely and qmake re-run before attempting to build.
The "editabletreemodel" Qt example, which is similar, builds without any trouble.
So, given the situtation as it is stated above, I am baffled. :crying:
I certainly appreciate your help.
~travlr :)
h5model.h
#ifndef H5MODEL_H
#define H5MODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class H5Node;
class H5Model : public QAbstractItemModel
{
Q_OBJECT
public:
H5Model(QObject* parent=0);
~H5Model();
// readonly
QVariant data(const QModelIndex & index, int role) const;
Qt::ItemFlags flags(const QModelIndex & index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex & parent) const;
QModelIndex parent(const QModelIndex & index) const;
int rowCount(const QModelIndex & parent=QModelIndex()) const;
int columnCount(const QModelIndex & parent=QModelIndex()) const;
// resizeable
bool insertRows(int row, int count, const QModelIndex & parent=QModelIndex());
public slots:
void h5TreeClicked(const QModelIndex & index);
private:
// variables
H5Node* m_rootNode;
};
#endif // H5MODEL_H
h5model.cpp
#include <QtGui>
#include <QDebug>
#include "h5node.h"
#include "h5model.h"
H5Model::H5Model(QObject* parent) : QAbstractItemModel(parent)
{
m_rootNode = new H5Node(H5Node::Root, "/data/data", 0);
m_rootNode->loadChildren();
}
H5Model::~H5Model()
{
delete m_rootNode;
}
int H5Model::columnCount(const QModelIndex & parent) const
{
if (parent.isValid())
return static_cast<H5Node*>(parent.internalPointer())->columnCount();
else
return m_rootNode->columnCount();
}
QVariant H5Model::data(const QModelIndex & index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
H5Node* node = static_cast<H5Node*>(index.internalPointer());
return node->data(index.column());
}
Qt::ItemFlags H5Model::flags(const QModelIndex & index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant H5Model::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0)
return "Name";
else if (section == 1)
return "Type";
else
return QVariant();
}
return QVariant();
}
QModelIndex H5Model::index(int row, int column, const QModelIndex & parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
H5Node* parentItem;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
H5Node* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex H5Model::parent(const QModelIndex & index) const
{
if (!index.isValid())
return QModelIndex();
H5Node* childItem = static_cast<H5Node*>(index.internalPointer());
H5Node* parentItem = childItem->parent();
if (parentItem == m_rootNode)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int H5Model::rowCount(const QModelIndex & parent) const
{
H5Node* parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
return parentItem->childCount();
}
bool insertRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(row)
Q_UNUSED(count)
H5Node* node = static_cast<H5Node*>(parent.internalPointer());
int numChildren = node->loadChildren();
if (numChildren == 0) {
return false;
}
beginInsertRows(parent, 0, numChildren - 1);
node->loadChildren();
endInsertRows();
return true;
}
// slot
void H5Model::h5TreeClicked(const QModelIndex & index)
{
insertRows(0, 0, index);
}