• 7385阅读
  • 10回复

学习Model&View [复制链接]

上一主题 下一主题
离线alexltr
 

只看楼主 倒序阅读 楼主  发表于: 2011-04-17
最近在跟着C++ GUI Qt4第十章学习Model&View,这一章来来回回看了几遍,但还是很难理解透。

Qt预定义的那几个model大概还知道怎么使用,但对于自定义MODEL就很难理解,特别是data,setdata,flags等,分不清具体起什么作用。就拿Cities这个例子来说吧,我有下面两个问题

1. 怎样把里面的item设为checkable的? //这个问题我用下面的代码试了一下,但不行。
2. 当输入的数大于1000时,怎样把当前item改为checked?

谢谢。

  1. QVariant CityModel::data(const QModelIndex &index, int role) const
  2. {
  3. if (!index.isValid())
  4. return QVariant();
  5. if (role == Qt::CheckStateRole) {
  6. return QVariant(Qt::Unchecked); //我把例子原来语句改了,有BOX但不可以check.
  7. } else if (role == Qt::DisplayRole) {
  8. if (index.row() == index.column())
  9. return 0;
  10. int offset = offsetOf(index.row(), index.column());
  11. return distances[offset];
  12. }
  13. return QVariant();
  14. }
  15. bool CityModel::setData(const QModelIndex &index,
  16. const QVariant &value, int role)
  17. {
  18. if (index.isValid() && index.row() != index.column()
  19. && role == Qt::EditRole) {
  20. int offset = offsetOf(index.row(), index.column());
  21. distances[offset] = value.toInt();
  22. QModelIndex transposedIndex = createIndex(index.column(),index.row());
  23. emit dataChanged(index, index);
  24. emit dataChanged(transposedIndex, transposedIndex);
  25. return true;
  26. }
  27. return false;
  28. }
  29. QVariant CityModel::headerData(int section,
  30. Qt::Orientation /* orientation */,
  31. int role) const
  32. {
  33. if (role == Qt::DisplayRole)
  34. return cities[section];
  35. return QVariant();
  36. }
  37. Qt::ItemFlags CityModel::flags(const QModelIndex &index) const
  38. {
  39. Qt::ItemFlags flags = QAbstractItemModel::flags(index);
  40. if (index.row() != index.column())
  41. flags |= Qt::ItemIsEditable;
  42. flags |= Qt::ItemIsUserCheckable; //我加多了这一句!
  43. return flags;
  44. }



[ 此帖被alexltr在2011-04-21 23:33重新编辑 ]
附件: cities.rar (426 K) 下载次数:4
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线alexltr

只看该作者 1楼 发表于: 2011-04-17
这个版块好冷清喔,顶一下!!!
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线bluegem218

只看该作者 2楼 发表于: 2011-04-18
我也在学!这个有点麻烦。因为它分开了model view 还有那个delegate。灵活性增强了,但是有点难理解。
离线alexltr

只看该作者 3楼 发表于: 2011-04-18
再顶!!
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线alexltr

只看该作者 4楼 发表于: 2011-04-19
再顶!! !
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线alexltr

只看该作者 5楼 发表于: 2011-04-21
没人帮忙,还是自己慢慢学习VIEW & MODEL 吧。
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线alexltr

只看该作者 6楼 发表于: 2011-04-21
终于有点眉目了, 继续学习。

  1. QVariant CityModel::data(const QModelIndex &index, int role) const
  2. {
  3. if (!index.isValid())
  4. return QVariant();
  5. if (role == Qt::DisplayRole || role == Qt::CheckStateRole || role == Qt::BackgroundRole)
  6. {
  7. if (index.row() == index.column() && role == Qt::BackgroundRole)
  8. {
  9. return Qt::red;
  10. }
  11. if (role == Qt::DisplayRole || role == Qt::CheckStateRole)
  12. {
  13. int offset = offsetOf(index.row(),index.column());
  14. if (role == Qt::DisplayRole)
  15. {
  16. return distances[offset];
  17. }
  18. else if (role == Qt::CheckStateRole && index.row() != index.column())
  19. {
  20. return distances[offset]>1000 ? Qt::Checked : Qt::Unchecked;
  21. }
  22. }
  23. }
  24. return QVariant();
  25. }
  26. bool CityModel::setData(const QModelIndex &index,
  27. const QVariant &value, int role)
  28. {
  29. if (!index.isValid())
  30. return false;
  31. /*
  32. if (role == Qt::CheckStateRole)
  33. {
  34. return QAbstractTableModel::setData(index,value,Qt::CheckStateRole);
  35. }
  36. */
  37. if (index.row() != index.column() && role == Qt::EditRole)
  38. {
  39. int offset = offsetOf(index.row(),index.column());
  40. distances[offset] = value.toInt();
  41. emit dataChanged(index,index);
  42. return QAbstractTableModel::setData(index,value.toInt()>1000 ? Qt::Unchecked : Qt::Checked,Qt::CheckStateRole);
  43. }
  44. }
  45. QVariant CityModel::headerData(int section,
  46. Qt::Orientation /* orientation */,
  47. int role) const
  48. {
  49. if (role == Qt::DisplayRole)
  50. return cities[section];
  51. return QVariant();
  52. }
  53. Qt::ItemFlags CityModel::flags(const QModelIndex &index) const
  54. {
  55. Qt::ItemFlags flags = QAbstractItemModel::flags(index);
  56. flags |= Qt::ItemIsUserCheckable; //我加多了这一句!
  57. if (index.row() != index.column())
  58. flags |= Qt::ItemIsEditable;
  59. return flags;
  60. }

我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线wujianmei
只看该作者 7楼 发表于: 2011-05-17
学习学习
离线alexltr

只看该作者 8楼 发表于: 2011-05-17
<<C++ GUI QT4编程>>的第十章我已翻来覆去看了几遍。
现在正在跟API文档学<<Model/View Programming>>这一主题的内容,准备把这一主题的内容翻译成中文,就当是做笔记吧,已完成将近一半。
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线skyarmy
只看该作者 9楼 发表于: 2011-06-08
翻译的如何了?发上来看看啊!
离线oscarboycn

只看该作者 10楼 发表于: 2011-06-29
http://www.21ic.com
http://bbs.eetop.cn
http://www.eetop.cn/
http://www.eet-china.com/
http://www.netyi.net/
http://www.pcbbbs.com/
http://www.pcbtech.net/
快速回复
限100 字节
 
上一个 下一个