前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qt-自定义可编辑模型

Qt-自定义可编辑模型

作者头像
kdyonly
发布2023-03-03 19:20:05
3590
发布2023-03-03 19:20:05
举报
文章被收录于专栏:个人编程笔记个人编程笔记

浏览量 1

不管是哪种编程语言,在这里看不到一些系列的教程,当然也是由于笔者掌握的知识不够深,这里只会发送一些相关语言的实例,供大家参考学习。有时候笔者会分享一些自己觉得好的书籍,教学视频,或者好的学习网站给你们,这就需要你们有自学的能力,学习从来都不是件轻松的事,只有好好的坚持下去,才能成为大神。如果对于Qt有兴趣的伙伴,不妨去学习一下豆子博主的系列教程,会对你有所帮助。

代码语言:javascript
复制
//currencymodel.h
#ifndef CURRENCYMODEL_H
#define CURRENCYMODEL_H
#include <QAbstractTableModel>
#include <QVariant>
#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;
    //可编辑模型实现
    Qt::ItemFlags flags(const QModelIndex &index)const;
    bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
private:
    QString currencyAt(int offset) const;
    QMap<QString,double> currencyMap;
};
#endif 
// CURRENCYMODEL_H
//currencymodel.cpp
#include "currencymodel.h"
CurrencyModel::CurrencyModel(QObject *parent):QAbstractTableModel(parent)
{
}
//返回行数目
int CurrencyModel::rowCount(const QModelIndex &parent) const
{
    return currencyMap.count();
}
//返回列数目
int CurrencyModel::columnCount(const QModelIndex &parent) const
{
    return currencyMap.count();
}
//返回列名
//QVariant类将大部分类型(int,string)的数据封装起来,调用时使用to函数取出,例如:int类型包装成QVariant,用QVariant::toInt()取出
QVariant CurrencyModel::headerData(int section, Qt::Orientation, int role) const
{
    if(role !=Qt::DisplayRole){
        return QVariant();
    }
    return currencyAt(section);
}
QString CurrencyModel::currencyAt(int offset) const
{
    return (currencyMap.begin()+offset).key();
}
//设置底层的实际数据,由于不可能将数据硬编码,所以必须为模型提供一个用于设置的函数
void CurrencyModel::setCurrencyMap(const QMap<QString, double> &map)
{
    //两个函数为重置内部数据做准备
    beginResetModel();
    currencyMap=map;
    endResetModel();
}
//返回单元格数据(只读模型)
//QVariant CurrencyModel::data(const QModelIndex &index,int role) const
//{
//    if(!index.isValid()){
//        return QVariant();
//    }
//    if(role==Qt::TextAlignmentRole){
//        return int(Qt::DisplayRole | Qt::AlignCenter);
//    }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::data(const QModelIndex &index,int role) const
{
    if(!index.isValid()){
        return QVariant();
    }
    if(role==Qt::TextAlignmentRole){
        return int(Qt::DisplayRole | Qt::AlignCenter);
    }else if(role==Qt::DisplayRole || role==Qt::EditRole){
        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();
}
//可编辑模型函数实现
Qt::ItemFlags CurrencyModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags=QAbstractItemModel::flags(index);
    if(index.row()!=index.column()){
        //按位或
        flags |= Qt::ItemIsEditable;
    }
    return flags;
}
//更新数据
bool CurrencyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(index.isValid()&&index.row()!=index.column()&&role==Qt::EditRole){
        QString columnCurrency=headerData(index.column(),Qt::Horizontal,Qt::DisplayRole).toString();
        QString rowCurrency=headerData(index.row(),Qt::Vertical,Qt::DisplayRole).toString();
        currencyMap.insert(columnCurrency,value.toDouble()*currencyMap.value(rowCurrency));
        emit dataChanged(index,index);
        return true;
    }
    return false;
}
//main.cpp
#include <QApplication>
#include <currencymodel.h>
#include <QTableView>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMap<QString,double> data;
    data["USD"]=1.0000;
    data["CNY"]=0.1628;
    data["GBP"]=1.5361;
    data["EUR"]=1.2992;
    data["HKD"]=0.1289;
    QTableView view;
    CurrencyModel *model=new CurrencyModel(&view);
    model->setCurrencyMap(data);
    view.setModel(model);
    view.resize(400,300);
    view.show();
    return a.exec();
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-11-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档