• 5962阅读
  • 9回复

[提问]Qt学习之路47里的问题 [复制链接]

上一主题 下一主题
离线零度
 

只看楼主 倒序阅读 楼主  发表于: 2014-02-26
   错误
          
       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::**下面都是红线麻烦前辈们指点一下。。。
本帖提到的人: @XChinux @jdwx @彩阳
离线realfan

只看该作者 1楼 发表于: 2014-02-26
头文件中加上
#include <QString>
#include <QList>

离线realfan

只看该作者 2楼 发表于: 2014-02-26
case Node::Root;
后面是冒号,不是分号
离线零度

只看该作者 3楼 发表于: 2014-02-26
      switch语句里的分号换冒号了   在node.cpp里加了#include <QString>
#include <QList>  错误还是有   试了很多种方法也不行
错误:
      



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里的错误



离线realfan

只看该作者 4楼 发表于: 2014-02-27
回 零度 的帖子
零度:      switch语句里的分号换冒号了   在node.cpp里加了#include <QString>
#include <QList>  错误还是有   试了很多种方法也不行
错误:
      [图片]
. .. (2014-02-26 23:29) 

你在头文件中用到了QList对象,所以,要在头文件中#include <QList>
离线零度

只看该作者 5楼 发表于: 2014-02-27
回 realfan 的帖子
realfan:你在头文件中用到了QList对象,所以,要在头文件中#include <QList> (2014-02-27 08:38) 

   以前把#include <QList>放.cpp文件里了有人指点说放.h文件里就对了   可是还有一个问题
错误是:uninitialized reference member 'Node::str'    
在.cpp文件里:
Node::Node(Type type,const QString &str)错误提示的位置
{
    this->type = type;
    this->str = str;
    parent = 0;
}

在.h文件里定义了
Node(Type type, const QString &str = "");
    ~Node();

    Type type;
    QString &str;
    Node *parent;
    QList<Node *>children;
为什么还是不行。。。






























































































离线realfan

只看该作者 6楼 发表于: 2014-02-28
构造传入的参数是const QString &str,那成员变量也要是const QString &才行,而不能是QString &str
另外,初始化必须以列表方式初始化
Node::Node(Type type,const QString &str) : str(str)
{
    this->type = type;
    //this->str = str;   //引用,不能在这初始化
    parent = 0;
}
离线realfan

只看该作者 7楼 发表于: 2014-02-28
回 零度 的帖子
零度:
   以前把#include <QList>放.cpp文件里了有人指点说放.h文件里就对了   可是还有一个问题
错误是:uninitialized reference member 'Node::str'    
在.cpp文件里:
Node::Node(Type type,const QString &str)错误提示的位置
.......

构造传入的参数是const QString &str,那成员变量也要是const QString &才行,而不能是QString &str
另外,初始化必须以列表方式初始化
Node::Node(Type type,const QString &str) : str(str)
{
    this->type = type;
    //this->str = str;   //引用,不能在这初始化
    parent = 0;
}
离线noidea

只看该作者 8楼 发表于: 2014-02-28
回 realfan 的帖子
realfan:构造传入的参数是const QString &str,那成员变量也要是const QString &才行,而不能是QString &str
另外,初始化必须以列表方式初始化
Node::Node(Type type,const QString &str) : str(str)
{
....... (2014-02-28 08:43) 

构造函数成员的初始化有两种方式,一个是构造列表,一个是在构造函数。
区别是构造时间不同。

你可以参见Effective C++,里边有详细提到为什么推荐使用构造参数列表。
如果遇到IO读写等特殊问题,还就必须放在构造函数函数体内部。
talk in code
Qt5.2.1 + MacOSX10.9
离线零度

只看该作者 9楼 发表于: 2014-03-01
  谢谢两位前辈的指点   我又知道了一点
快速回复
限100 字节
 
上一个 下一个