• 4415阅读
  • 2回复

自定义表格模型,列标题内容显示不正确 [复制链接]

上一主题 下一主题
离线compumatic
 

只看楼主 倒序阅读 楼主  发表于: 2012-08-29
采用C++ GUI Qt 4书中的源码例子,数据显示正确,列标题显示1,2,3,4,源码如下
  1. #ifndef CURRENCYMODEL_H   
  2. #define CURRENCYMODEL_H   
  3.   
  4. #include <QAbstractTableModel>   
  5. #include <QMap>   
  6.   
  7. class CurrencyModel : public QAbstractTableModel  
  8. {  
  9. public:  
  10.     CurrencyModel(QObject *parent = 0);  
  11.   
  12.     void setCurrencyMap(const QMap<QString, double> &map);  
  13.     int rowCount(const QModelIndex &parent) const;  
  14.     int columnCount(const QModelIndex &parent) const;  
  15.     QVariant data(const QModelIndex &index, int role) const;  
  16.     QVariant headerData(int section, Qt::Orientation orientation,  
  17.                         int role) const;  
  18.   
  19. private:  
  20.     QString currencyAt(int offset) const;  
  21.   
  22.     QMap<QString, double> currencyMap;  
  23. };  
  24.   
  25. #endif  
  1. #include <QtCore>   
  2.   
  3. #include "currencymodel.h"   
  4.   
  5. CurrencyModel::CurrencyModel(QObject *parent)  
  6.     : QAbstractTableModel(parent)  
  7. {  
  8. }  
  9.   
  10. void CurrencyModel::setCurrencyMap(const QMap<QString, double> &map)  
  11. {  
  12.     currencyMap = map;  
  13.     //重置模型至原始状态,告诉所有视图,他们数据都无效,强制刷新数据   
  14.     reset();  
  15. }  
  16.   
  17. //返回行数   
  18. int CurrencyModel::rowCount(const QModelIndex & /* parent */) const  
  19. {  
  20.     return currencyMap.count();  
  21. }  
  22. //返回列数   
  23. int CurrencyModel::columnCount(const QModelIndex & /* parent */) const  
  24. {  
  25.     return currencyMap.count();  
  26. }  
  27.   
  28. //返回一个项的任意角色的值,这个项被指定为QModelIndex   
  29. QVariant CurrencyModel::data(const QModelIndex &index, int role) const  
  30. {  
  31.     if (!index.isValid())  
  32.         return QVariant();  
  33.   
  34.     if (role == Qt::TextAlignmentRole) {  
  35.         return int(Qt::AlignRight | Qt::AlignVCenter);  
  36.     } else if (role == Qt::DisplayRole) {  
  37.         QString rowCurrency = currencyAt(index.row());  
  38.         QString columnCurrency = currencyAt(index.column());  
  39.   
  40.         if (currencyMap.value(rowCurrency) == 0.0)  
  41.             return "####";  
  42.   
  43.         double amount = currencyMap.value(columnCurrency)  
  44.                         / currencyMap.value(rowCurrency);  
  45.   
  46.         return QString("%1").arg(amount, 0, 'f', 4);  
  47.     }  
  48.     return QVariant();  
  49. }  
  50. //返回表头名称,(行号或列号,水平或垂直,角色)   
  51. QVariant CurrencyModel::headerData(int section,  
  52.                                    Qt::Orientation /* orientation */,  
  53.                                    int role) const  
  54. {  
  55.     if (role != Qt::DisplayRole)  
  56.         return QVariant();  
  57.     return currencyAt(section);  
  58. }  
  59. //获取当前关键字   
  60. QString CurrencyModel::currencyAt(int offset) const  
  61. {  
  62.     return (currencyMap.begin() + offset).key();  
  63. }  
  1. #include <QtGui>   
  2.   
  3. #include "currencymodel.h"   
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication app(argc, argv);  
  8.   
  9.     //数据源   
  10.     QMap<QString, double> currencyMap;  
  11.     currencyMap.insert("AUD", 1.3259);  
  12.     currencyMap.insert("CHF", 1.2970);  
  13.     currencyMap.insert("CZK", 24.510);  
  14.     currencyMap.insert("DKK", 6.2168);  
  15.   
  16.     //自定义表模型   
  17.     CurrencyModel *currencyModel=new CurrencyModel ;  
  18.     currencyModel->setCurrencyMap(currencyMap);  
  19.     //表视图   
  20.     QTableView tableView;  
  21.     //设置视图模型   
  22.     tableView.setModel(currencyModel);  
  23.      tableView.show();  
  24.   
  25.     return app.exec();  
  26. }  
离线compumatic

只看该作者 1楼 发表于: 2012-08-29
没人么?
离线alexltr

只看该作者 2楼 发表于: 2012-08-29
试过,没有问题啊!
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
快速回复
限100 字节
 
上一个 下一个