错误:
node.h
#ifndef NODE_H
#define NODE_H
#include "node.h"
class Type;
class
QString;//这两种方法也不行
class Node
{
public:
enum Type
{
Root,
OrExpression,
AndExpression,
NotExpression,
Atom,
Identifier,
Operator,
Punctuator
};
Node(Type type, const QString &str = " ");
~Node();
Type type;
QString str;
Node *parent;
QList<Node *>children;
};
#endif //NODE_H
node.cpp
#include "node.h"
Node::Node(Type type,const QString &str)
{
this->type = type;
this->str = str;
parent = 0;
}
Node::~Node()
{
qDeleteAll(children);
}
booleanmodel.h
#ifndef BOOLEANMODEL_H
#define BOOLEANMODEL_H#include <QAbstractItemModel>
#include "node.h"
class BooleanModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit BooleanModel(
QObject *parent = 0);
void setRootNode(Node *node);
QModelIndex index(int row, int column,
const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role) const;private:
Node *nodeFromIndex(const QModelIndex &index) const;
Node *rootNode;
signals:
public slots:};
#endif // BOOLEANMODEL_H
booleanmodel.cpp
#include "booleanmodel.h"
#include "node.h"BooleanModel::BooleanModel(QObject *parent) :
QAbstractItemModel(parent)
{
rootNode = 0;
}
BooleanModel::~BooleanModel()
{
delete rootNode;
}void BooleanModel::setRootNode(Node *node)
{
delete rootNode;
rootNode = node;
reset();
}
QModelIndex BooleanModel::index(int row, int column, const QModelIndex &parent) const
{
if (!rootNode || row < 0 || column < 0)
{
return QModelIndex();
Node *parentNode = nodeFromIndex(parent);
Node *childNode = parentNode->children.value(row);
} if (!childNode)
{
return QModelIndex();
return createIndex(row,column,childNode);
}
}
Node *BooleanModel::nodeFromIndex(const QModelIndex &index) const
{
if (index.isValid())
{
return static_cast<Node*>(index.internalPointer());
}
else
{
return rootNode;
}
}int BooleanModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
{
return 0;
}
Node *parentNode = nodeFromIndex(parent);
if (!parentNode)
{
return 0;
return parentNode->children.count();
}
}int BooleanModel::columnCount(const QModelIndex & /* parent */)
{
return 2;
}
QModelIndex BooleanModel::parent(const QModelIndex &child) const
{
Node *node = nodeFromIndex(child);
if (!node)
{
return QModelIndex(); }
Node *parentNode = node->parent;
if (!parentNode)
{
return QModelIndex();
}
Node *grandparentNode = parentNode->parent;
if (!grandparentNode)
{
return QModelIndex();
int row = grandparentNode->children.indexOf(parentNode);
return createIndex(row,0,parentNode);
}
}QVariant BooleanModel::data(const QModelIndex &index, int role) const
{
if (role != Qt::DisplayRole)
{
return QVariant();
}
Node *node = nodeFromIndex(index);
if (!node)
{
return QVariant();
}
if (index.column() == 0)
{
switch ( node->type)
{
case Node::Root;
return tr("Root");
case Node::OrExpression;
return tr("OR Expression");
case Node::AndExpression;
return tr("AND Expression");
case Node::NotExpression;
return tr("NOT Expression");
case Node::Atom;
return tr("Atom");
case Node::Identifier;
return tr("Identifier");
case Node::Operator;
return tr("Operator");
case Node::Punctuator;
return tr("Punctuator");
default:
return tr("Unknown");
}
}
else if(index.column() == 1)
{
return node->str;
}
return QVariant();
}QVariant BooleanModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole
{
if(section == 0)
{
return tr("Node");
}
else if (section == 1)
{
return tr("Vaiue");
}
}
return QVariant();
}
switch语句里每个case Node::**下面都是红线麻烦前辈们指点一下。。。