Hi to all, I've centos6.4 in VM AND Qt5.2.1 I am building a project given in book "C++-GUI-Programming-with-Qt-4-1st-ed.pdf". but in book it is by coding and i am building by using GUI.
here is my cell.h file :-
Qt Code:
  1. #ifndef CELL_H
  2. #define CELL_H
  3.  
  4. #include <QTableWidgetItem>
  5. #include <QWidget>
  6.  
  7. class Cell : public QTableWidgetItem
  8. {
  9.  
  10. public:
  11. Cell();
  12.  
  13. QTableWidgetItem *clone() const;
  14. void setData(int role, const QVariant &value);
  15. QVariant data(int role) const;
  16. void setFormula(const QString &formula);
  17. QString formula() const;
  18. void setDirty();
  19.  
  20. private:
  21. QVariant value() const;
  22. QVariant evalExpression(const QString &str, int &pos) const;
  23. QVariant evalTerm(const QString &str, int &pos) const;
  24. QVariant evalFactor(const QString &str, int &pos) const;
  25.  
  26. mutable QVariant cachedValue;
  27. mutable bool cacheIsDirty;
  28. };
To copy to clipboard, switch view to plain text mode 
and here is its cell.cpp file :-
Qt Code:
  1. #include "cell.h"
  2. #include <QtWidgets>
  3.  
  4. Cell::Cell()
  5. {
  6. setDirty();
  7. }
  8.  
  9. QTableWidgetItem *Cell::clone() const
  10. {
  11. return new Cell(*this);
  12. }
  13.  
  14. void Cell::setData(int role, const QVariant &value)
  15. {
  16. QTableWidgetItem::setData(role, value);
  17. if(role == Qt::EditRole)
  18. {
  19. setDirty();
  20. }
  21. }
  22.  
  23. QString Cell::formula() const
  24. {
  25. return data(Qt::EditRole).toString();
  26. }
  27.  
  28. const QVariant Invalid;
  29.  
  30. QVariant Cell::data(int role) const
  31. {
  32. if(role == Qt::DisplayRole)
  33. {
  34. if(this->value().isValid())
  35. return value().toString();
  36. else
  37. return "####";
  38. }
  39. else if(role == Qt::TextAlignmentRole)
  40. {
  41. if(this->value().type() == QVariant::String)
  42. return int(Qt::AlignCenter| Qt::AlignVCenter);
  43. else
  44. return int(Qt::AlignRight | Qt::AlignVCenter);
  45. }
  46. else
  47. {
  48. return QTableWidgetItem::data(role);
  49. }
  50. }
  51.  
  52. void Cell::setFormula(const QString &formula)
  53. {
  54. setData(Qt::EditRole, formula);
  55. }
  56.  
  57. void Cell::setDirty()
  58. {
  59. this->cacheIsDirty = true;
  60. }
  61.  
  62. QVariant Cell::value() const
  63. {
  64. if (cacheIsDirty)
  65. {
  66. cacheIsDirty = false;
  67.  
  68. QString formulaStr = formula();
  69. if (formulaStr.startsWith('\''))
  70. {
  71. cachedValue = formulaStr.mid(1);
  72. }
  73. else if (formulaStr.startsWith('='))
  74. {
  75. cachedValue = Invalid;
  76. QString expr = formulaStr.mid(1);
  77. expr.replace(" ", "");
  78. expr.append(QChar::Null);
  79.  
  80. int pos = 0;
  81. cachedValue = evalExpression(expr, pos);
  82. if (expr[pos] != QChar::Null)
  83. cachedValue = Invalid;
  84. }
  85. else
  86. {
  87. bool ok;
  88. double d = formulaStr.toDouble(&ok);
  89. if (ok)
  90. {
  91. cachedValue = d;
  92. }
  93. else
  94. {
  95. cachedValue = formulaStr;
  96. }
  97. }
  98. }
  99. return cachedValue;
  100. }
  101.  
  102. QVariant Cell::evalExpression(const QString &str, int &pos) const
  103. {
  104. QVariant result = evalTerm(str, pos);
  105. while (str[pos] != QChar::Null)
  106. {
  107. QChar op = str[pos];
  108. if (op != '+' && op != '-')
  109. return result;
  110. ++pos;
  111.  
  112. QVariant term = evalTerm(str, pos);
  113. if (result.type() == QVariant::Double
  114. && term.type() == QVariant::Double)
  115. {
  116. if (op == '+')
  117. {
  118. result = result.toDouble() + term.toDouble();
  119. }
  120. else
  121. {
  122. result = result.toDouble() - term.toDouble();
  123. }
  124. }
  125. else
  126. {
  127. result = Invalid;
  128. }
  129. }
  130. return result;
  131. }
  132.  
  133. QVariant Cell::evalTerm(const QString &str, int &pos) const
  134. {
  135. QVariant result = evalFactor(str, pos);
  136. while (str[pos] != QChar::Null)
  137. {
  138. QChar op = str[pos];
  139. if (op != '*' && op != '/')
  140. return result;
  141. ++pos;
  142.  
  143. QVariant factor = evalFactor(str, pos);
  144. if (result.type() == QVariant::Double
  145. && factor.type() == QVariant::Double)
  146. {
  147. if (op == '*')
  148. {
  149. result = result.toDouble() * factor.toDouble();
  150. }
  151. else
  152. {
  153. if (factor.toDouble() == 0.0)
  154. {
  155. result = Invalid;
  156. }
  157. else
  158. {
  159. result = result.toDouble() / factor.toDouble();
  160. }
  161. }
  162. }
  163. else
  164. {
  165. result = Invalid;
  166. }
  167. }
  168. return result;
  169. }
  170.  
  171.  
  172. QVariant Cell::evalFactor(const QString &str, int &pos) const
  173. {
  174. QVariant result;
  175. bool negative = false;
  176.  
  177. if (str[pos] == '-')
  178. {
  179. negative = true;
  180. ++pos;
  181. }
  182.  
  183. if (str[pos] == '(')
  184. {
  185. ++pos;
  186. result = evalExpression(str, pos);
  187. if (str[pos] != ')')
  188. result = Invalid;
  189. ++pos;
  190. }
  191. else
  192. {
  193. QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
  194. QString token;
  195.  
  196. while (str[pos].isLetterOrNumber() || str[pos] == '.')
  197. {
  198. token += str[pos];
  199. ++pos;
  200. }
  201.  
  202. if (regExp.exactMatch(token))
  203. {
  204. int column = token[0].toUpper().unicode() - 'A';
  205. int row = token.mid(1).toInt() - 1;
  206.  
  207. Cell *c = static_cast<Cell *>(
  208. tableWidget()->item(row, column));
  209. if (c)
  210. {
  211. result = c->value();
  212. }
  213. else
  214. {
  215. result = 0.0;
  216. }
  217. }
  218. else
  219. {
  220. bool ok;
  221. result = token.toDouble(&ok);
  222. if (!ok)
  223. result = Invalid;
  224. }
  225. }
  226.  
  227. if (negative)
  228. {
  229. if (result.type() == QVariant::Double)
  230. {
  231. result = -result.toDouble();
  232. }
  233. else
  234. {
  235. result = Invalid;
  236. }
  237. }
  238. return result;
  239. }
To copy to clipboard, switch view to plain text mode 
output is :- spsheet_mine.png
and output should be :- spsheet_book.png

of course i have not implemented toolbar but remaining should work.
how to remove error :-
No relevant classes found. No output generated