Qt-自定义可编辑模型

2023-03-03 19:39:00 浏览数 (1)

浏览量 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
代码语言:javascript复制
//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;
}
代码语言:javascript复制
//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();
}

0 人点赞