找回密码
 立即注册
收起左侧

求助,关于qt moc系统

4
回复
5383
查看
[复制链接]
累计签到:1 天
连续签到:1 天
来源: 2014-7-24 16:31:40 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
这几天闲着无事,就看了一下《C++ GUI Qt4编程》这本书,顺便敲了下第三章那个表格的例子,只敲了一部分,代码如下
cell.h:
  1. #ifndef CELL_H
  2. #define CELL_H

  3. #include <QTableWidgetItem>
  4. #include <QtGui>

  5. class Cell : public QTableWidgetItem
  6. {
  7. public:
  8.     Cell();

  9.     QTableWidgetItem *clone() const;
  10.     void setData(int role, const QVariant &value);
  11.     QVariant data(int role) const;
  12.     void setFormula(const QString &formula);
  13.     QString formula() const;
  14.     void setDirty();

  15. private:
  16.     QVariant value() const;
  17.     QVariant evalExpression(const QString &str, int &pos) const;
  18.     QVariant evalTerm(const QString &str, int &pos) const;
  19.     QVariant evalFactor(const QString &str, int &pos) const;

  20.     mutable QVariant cachedValue;
  21.     mutable bool cacheIsDirty;
  22. };

  23. #endif
复制代码
cell.cpp:
  1. #include <QtGui>

  2. #include "cell.h"

  3. Cell::Cell()
  4. {
  5.     setDirty();
  6. }

  7. QTableWidgetItem *Cell::clone() const
  8. {
  9.     return new Cell(*this);
  10. }

  11. void Cell::setData(int role, const QVariant &value)
  12. {
  13.     QTableWidgetItem::setData(role, value);
  14.     if (role == Qt::EditRole)
  15.         setDirty();
  16. }

  17. QVariant Cell::data(int role) const
  18. {
  19.     if (role == Qt::DisplayRole) {
  20.         if (value().isValid()) {
  21.             return value().toString();
  22.         } else {
  23.             return "####";
  24.         }
  25.     } else if (role == Qt::TextAlignmentRole) {
  26.         if (value().type() == QVariant::String) {
  27.             return int(Qt::AlignLeft | Qt::AlignVCenter);
  28.         } else {
  29.             return int(Qt::AlignRight | Qt::AlignVCenter);
  30.         }
  31.     } else {
  32.         return QTableWidgetItem::data(role);
  33.     }
  34. }

  35. void Cell::setFormula(const QString &formula)
  36. {
  37.     setData(Qt::EditRole, formula);
  38. }

  39. QString Cell::formula() const
  40. {
  41.     return data(Qt::EditRole).toString();
  42. }

  43. void Cell::setDirty()
  44. {
  45.     cacheIsDirty = true;
  46. }

  47. const QVariant Invalid;

  48. QVariant Cell::value() const
  49. {
  50.     if (cacheIsDirty) {
  51.         cacheIsDirty = false;

  52.         QString formulaStr = formula();
  53.         if (formulaStr.startsWith('\'')) {
  54.             cachedValue = formulaStr.mid(1);
  55.         } else if (formulaStr.startsWith('=')) {
  56.             cachedValue = Invalid;
  57.             QString expr = formulaStr.mid(1);
  58.             expr.replace(" ", "");
  59.             expr.append(QChar::Null);

  60.             int pos = 0;
  61.             cachedValue = evalExpression(expr, pos);
  62.             if (expr[pos] != QChar::Null)
  63.                 cachedValue = Invalid;
  64.         } else {
  65.             bool ok;
  66.             double d = formulaStr.toDouble(&ok);
  67.             if (ok) {
  68.                 cachedValue = d;
  69.             } else {
  70.                 cachedValue = formulaStr;
  71.             }
  72.         }
  73.     }
  74.     return cachedValue;
  75. }

  76. QVariant Cell::evalExpression(const QString &str, int &pos) const
  77. {
  78.     QVariant result = evalTerm(str, pos);
  79.     while (str[pos] != QChar::Null) {
  80.         QChar op = str[pos];
  81.         if (op != '+' && op != '-')
  82.             return result;
  83.         ++pos;

  84.         QVariant term = evalTerm(str, pos);
  85.         if (result.type() == QVariant::Double
  86.                 && term.type() == QVariant::Double) {
  87.             if (op == '+') {
  88.                 result = result.toDouble() + term.toDouble();
  89.             } else {
  90.                 result = result.toDouble() - term.toDouble();
  91.             }
  92.         } else {
  93.             result = Invalid;
  94.         }
  95.     }
  96.     return result;
  97. }

  98. QVariant Cell::evalTerm(const QString &str, int &pos) const
  99. {
  100.     QVariant result = evalFactor(str, pos);
  101.     while (str[pos] != QChar::Null) {
  102.         QChar op = str[pos];
  103.         if (op != '*' && op != '/')
  104.             return result;
  105.         ++pos;

  106.         QVariant factor = evalFactor(str, pos);
  107.         if (result.type() == QVariant::Double
  108.                 && factor.type() == QVariant::Double) {
  109.             if (op == '*') {
  110.                 result = result.toDouble() * factor.toDouble();
  111.             } else {
  112.                 if (factor.toDouble() == 0.0) {
  113.                     result = Invalid;
  114.                 } else {
  115.                     result = result.toDouble() / factor.toDouble();
  116.                 }
  117.             }
  118.         } else {
  119.             result = Invalid;
  120.         }
  121.     }
  122.     return result;
  123. }

  124. QVariant Cell::evalFactor(const QString &str, int &pos) const
  125. {
  126.     QVariant result;
  127.     bool negative = false;

  128.     if (str[pos] == '-') {
  129.         negative = true;
  130.         ++pos;
  131.     }

  132.     if (str[pos] == '(') {
  133.         ++pos;
  134.         result = evalExpression(str, pos);
  135.         if (str[pos] != ')')
  136.             result = Invalid;
  137.         ++pos;
  138.     } else {
  139.         QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
  140.         QString token;

  141.         while (str[pos].isLetterOrNumber() || str[pos] == '.') {
  142.             token += str[pos];
  143.             ++pos;
  144.         }

  145.         if (regExp.exactMatch(token)) {
  146.             int column = token[0].toUpper().unicode() - 'A';
  147.             int row = token.mid(1).toInt() - 1;

  148.             Cell *c = static_cast<Cell *>(
  149.                               tableWidget()->item(row, column));
  150.             if (c) {
  151.                 result = c->value();
  152.             } else {
  153.                 result = 0.0;
  154.             }
  155.         } else {
  156.             bool ok;
  157.             result = token.toDouble(&ok);
  158.             if (!ok)
  159.                 result = Invalid;
  160.         }
  161.     }

  162.     if (negative) {
  163.         if (result.type() == QVariant::Double) {
  164.             result = -result.toDouble();
  165.         } else {
  166.             result = Invalid;
  167.         }
  168.     }
  169.     return result;
  170. }
复制代码
使用的是Qt5.2.1+Vs2012;发现报错,并且错误信息指向Qt元对象系统生成的moc开头的cpp文件,求解详情
回复

使用道具 举报

累计签到:6 天
连续签到:1 天
2014-7-25 01:07:58 显示全部楼层
最好把错误信息也贴上来
回复 支持 反对

使用道具 举报

累计签到:1 天
连续签到:1 天
2014-7-25 08:56:35 显示全部楼层
错误信息如下

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 支持 反对

使用道具 举报

累计签到:1 天
连续签到:1 天
2014-7-25 11:25:07 显示全部楼层
经过试验,把类定义中的Q_OBJECT宏去掉就能编译通过,但是还是不知道具体原因
回复 支持 反对

使用道具 举报

累计签到:1 天
连续签到:1 天
2014-7-25 11:33:58 显示全部楼层
经查看qtablewidget.h后发现,QTableWidgetItem不是QObject的子类,所以不能添加Q_OBJECT宏
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了