Qt-自定义可编辑模型
Qt 模型 自定义 编辑
2023-06-13 09:15:29 时间
浏览量 1
不管是哪种编程语言,在这里看不到一些系列的教程,当然也是由于笔者掌握的知识不够深,这里只会发送一些相关语言的实例,供大家参考学习。有时候笔者会分享一些自己觉得好的书籍,教学视频,或者好的学习网站给你们,这就需要你们有自学的能力,学习从来都不是件轻松的事,只有好好的坚持下去,才能成为大神。如果对于Qt有兴趣的伙伴,不妨去学习一下豆子博主的系列教程,会对你有所帮助。
//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();
}
相关文章
- qt中connect函数信号写法_qt中connect函数用法
- 树莓派基于QT实现利用USB转485模块进行串口通讯「建议收藏」
- [QT] QMap使用详解
- Qt浅谈之七:抽奖软件(可显示图片和姓名)
- 怎么判断摄像头开没开_qt获取控制台输出并显示
- qtabwidget tab样式_qt geometry
- QT配置opencv_qt调用opencv
- 二、Qt定时器与文本编辑器制作《QT 入门到实战》
- C/C++ Qt ListWidget 增加右键菜单
- Qt自绘系列-放大镜
- 编程在Linux下学习Qt界面编程(linux下qt界面)
- Linux串口应用开发实践——Qt篇(linux串口qt)
- Linux 环境下运用 Qt 编程的秘籍(linux下的qt编程)
- Linux下Qt环境快速安装指南(linux下qt安装)
- Linux环境下Qt交叉编译实战(linux交叉编译qt)
- Qt下Linux系统的输入法研究(qt输入法linux)
- 实现Linux下Qt应用静态编译的方法(linux下qt静态编译)
- 基于 Qt/Linux 平台的跨平台移植研究(qtlinux移植)
- Linux系统下Qt环境搭建指南(linuxqt环境搭建)
- Qt与Oracle表的数据操作指南(qtoracle表)
- 从零开始:Qt 编译在 Linux 上的指南(qt编译linux)
- Qt在Linux环境中的新技术应用(qt与linux程序设计)
- Linux 下 Qt 安装教程(qt安装教程linux)
- 程序Qt与MySQL驱动程序:让您实现无缝连接(qtmysql驱动)
- 使用QT连接MySQL数据库(qt数据库mysql)
- 深入浅出:使用Qt连接MSSQL(qt 连接mssql)
- 轻松实现QT直连Microsoft SQL Server(qt直连mssql)