自定义表格模型,列标题内容显示不正确

weixin_38052602 2019-09-19 04:25:39
采用C++ GUI Qt 4书中的源码例子,数据显示正确,列标题显示1,2,3,4,源码如下#ifndef CURRENCYMODEL_H   #define CURRENCYMODEL_H     #include <QAbstractTableModel>   #include <QMap>     class CurrencyModel : public QAbstractTableModel   {   public:       CurrencyModel(QObject *parent = 0);         void setCurrencyMap(const QMap<QString, double> &map);       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:       QString currencyAt(int offset) const;         QMap<QString, double> currencyMap;   };     #endif  #include <QtCore>     #include "currencymodel.h"     CurrencyModel::CurrencyModel(QObject *parent)       : QAbstractTableModel(parent)   {   }     void CurrencyModel::setCurrencyMap(const QMap<QString, double> &map)   {       currencyMap = map;       //重置模型至原始状态,告诉所有视图,他们数据都无效,强制刷新数据       reset();   }     //返回行数   int CurrencyModel::rowCount(const QModelIndex & /* parent */) const  {       return currencyMap.count();   }   //返回列数   int CurrencyModel::columnCount(const QModelIndex & /* parent */) const  {       return currencyMap.count();   }     //返回一个项的任意角色的值,这个项被指定为QModelIndex   QVariant CurrencyModel::data(const QModelIndex &index, int role) const  {       if (!index.isValid())           return QVariant();         if (role == Qt::TextAlignmentRole) {           return int(Qt::AlignRight | Qt::AlignVCenter);       } else if (role == Qt::DisplayRole) {           QString rowCurrency = currencyAt(index.row());           QString columnCurrency = currencyAt(index.column());             if (currencyMap.value(rowCurrency) == 0.0)               return "####";             double amount = currencyMap.value(columnCurrency)                           / currencyMap.value(rowCurrency);             return QString("%1").arg(amount, 0, 'f', 4);       }       return QVariant();   }   //返回表头名称,(行号或列号,水平或垂直,角色)   QVariant CurrencyModel::headerData(int section,                                      Qt::Orientation /* orientation */,                                      int role) const  {       if (role != Qt::DisplayRole)           return QVariant();       return currencyAt(section);   }   //获取当前关键字   QString CurrencyModel::currencyAt(int offset) const  {       return (currencyMap.begin() + offset).key();   }  #include <QtGui>     #include "currencymodel.h"     int main(int argc, char *argv[])   {       QApplication app(argc, argv);         //数据源       QMap<QString, double> currencyMap;       currencyMap.insert("AUD", 1.3259);       currencyMap.insert("CHF", 1.2970);       currencyMap.insert("CZK", 24.510);       currencyMap.insert("DKK", 6.2168);          //自定义表模型       CurrencyModel *currencyModel=new CurrencyModel ;       currencyModel->setCurrencyMap(currencyMap);       //表视图       QTableView tableView;       //设置视图模型       tableView.setModel(currencyModel);        tableView.show();         return app.exec();   }
...全文
22 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧