当前位置: 首页 > news >正文

福州企业建站服务全网模板建站系统

福州企业建站服务,全网模板建站系统,南宁网站推广流程,电销crm管理系统1.简介 QAbstractListModel是Qt框架中的一个抽象类,用于实现数据模型,用于在Qt的视图组件中展示和编辑列表数据。与QAbstractTableModel类似,它也是一个抽象类,提供了一些基本的接口和默认实现,可以方便地创建自定义的…

1.简介

QAbstractListModel是Qt框架中的一个抽象类,用于实现数据模型,用于在Qt的视图组件中展示和编辑列表数据。与QAbstractTableModel类似,它也是一个抽象类,提供了一些基本的接口和默认实现,可以方便地创建自定义的列表数据模型。

QAbstractListModel的主要功能包括以下几点:

  • 数据的获取和设置:通过实现data()和setData()接口,可以用于获取和设置列表中的数据。可以根据自己的数据结构和逻辑,在这两个接口中进行相关的操作。data()方法用于获取指定索引位置的数据,setData()方法用于设置指定索引位置的数据。
  • 列表项的管理:可以通过rowCount()方法获取列表中的项数。也可以通过insertRows()和removeRows()方法动态地增加或删除列表项。
  • 列表的显示和编辑:可以通过实现displayRole和editRole相关方法来确定列表数据在视图中的显示和编辑方式。也可以通过实现flags()方法来指定每个列表项的编辑属性。
  • 数据的排序:可以通过实现sort()方法来对列表中的数据进行排序。

由于该模型提供了比QAbstractItemModel更专业的接口,因此不适合与树视图一起使用;如果您想提供一个用于此目的的模型,则需要对QAbstractItemModel进行子类化。如果您需要使用多个列表模型来管理数据,则可能更适合使用子类QAbstractTableModel。

继承QAbstractListModel,需要重写rowCount()、data()、insertRows()、removeRows()等函数。

  • rowCount()函数返回模型的行数。
  • data()函数返回指定索引处的数据。
  • insertRows()插入行
  • removeRows()删除行

2.示例

声明数据结构体:

typedef struct _student
{QString name;int age;double score;
}Student;

重写rowCount()、data()、insertRows()和removeRows()等函数。 

#ifndef MYLISTMODEL_H
#define MYLISTMODEL_H#include <QAbstractListModel>
#include <QObject>
#include <QList>typedef struct _student
{QString name;int age;double score;
}Student;class MyListModel : public QAbstractListModel
{Q_OBJECT
public:MyListModel(QObject *parent = nullptr);enum RoleNames{Name,Age,Score};public://更新void update(QList<Student> students);// 返回列表中行的数量virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;// 返回指定索引处的数据virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;//插入行virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());//删除行virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());private:QList<Student> m_lstStu;
};#endif // MYLISTMODEL_H#include "MyListModel.h"MyListModel::MyListModel(QObject *parent): QAbstractListModel(parent)
{}void MyListModel::update(QList<Student> students)
{m_lstStu = students;for(int i=0;i<m_lstStu.size();i++){beginInsertRows(QModelIndex(),i,i);endInsertRows();}
}int MyListModel::rowCount(const QModelIndex &parent) const
{Q_UNUSED(parent);return m_lstStu.size();
}QVariant MyListModel::data(const QModelIndex &index, int role) const
{if(!index.isValid())return QVariant();int nRow = index.row();Student stu = m_lstStu.at(nRow);if (role == Qt::DisplayRole || role == Qt::EditRole){QString ret = QString("%1_%2_%3").arg(stu.name).arg(stu.age).arg(stu.score);return ret;}return QVariant();
}bool MyListModel::insertRows(int row, int count, const QModelIndex &parent)
{if (row >= 0 && row <= m_lstStu.size()){beginInsertRows(parent, row, row + count - 1);for (int i = 0; i < count; ++i){//插入一个空的数据Student stu;stu.name = QString();stu.age = 0;stu.score = 0;m_lstStu.insert(row, stu);}endInsertRows();return true;}return false;
}bool MyListModel::removeRows(int row, int count, const QModelIndex &parent)
{if (row >= 0 && row + count <= m_lstStu.size()){beginRemoveRows(parent, row, row + count - 1);for (int i = 0; i < count; ++i){m_lstStu.removeAt(row);}endRemoveRows();return true;}return false;
}

使用示例:

#include "ListForm.h"
#include "ui_ListForm.h"
#include "MyListModel.h"MyListModel *pModel = nullptr;ListForm::ListForm(QWidget *parent) :QWidget(parent),ui(new Ui::ListForm)
{ui->setupUi(this);//去除选中虚线框ui->listView->setFocusPolicy(Qt::NoFocus);//设置整行选中ui->listView->setSelectionBehavior(QAbstractItemView::SelectRows);pModel = new MyListModel(this);// 构造数据,更新界面QList<Student> students;QList<QString> nameList;nameList<<"张三"<<"李四"<<"王二"<<"赵五"<<"刘六";for (int i = 0; i < 5; ++i){Student student;student.name = nameList.at(i);student.age = qrand()%6 + 13;//随机生成13到19的随机数student.score = qrand()%20 + 80;//随机生成0到100的随机数;students.append(student);}pModel->update(students);ui->listView->setModel(pModel);
}ListForm::~ListForm()
{delete ui;
}void ListForm::on_btnInsert_clicked()
{if(!pModel)return;int row = ui->listView->currentIndex().row();if(row < 0)return;pModel->insertRows(row+1,1);
}void ListForm::on_btnDel_clicked()
{if(!pModel)return;int row = ui->listView->currentIndex().row();if(row < 0)return;pModel->removeRows(row,1);
}

3.推荐

Qt 继承QAbstractTableModel实现自定义TableModel-CSDN博客

Qt 插件开发详解_qt插件化开发-CSDN博客

Qt 继承QAbstractTableModel实现自定义TableModel-CSDN博客

 


文章转载自:
http://bioconversion.fzLk.cn
http://lithotritor.fzLk.cn
http://omphaloskepsis.fzLk.cn
http://lubricant.fzLk.cn
http://triumph.fzLk.cn
http://haikou.fzLk.cn
http://klan.fzLk.cn
http://birdie.fzLk.cn
http://spalato.fzLk.cn
http://preestablish.fzLk.cn
http://jeroboam.fzLk.cn
http://pipeless.fzLk.cn
http://raft.fzLk.cn
http://boustrophedon.fzLk.cn
http://bandana.fzLk.cn
http://licit.fzLk.cn
http://handbell.fzLk.cn
http://phenakite.fzLk.cn
http://ikunolite.fzLk.cn
http://unsummoned.fzLk.cn
http://okefenokee.fzLk.cn
http://sparaxis.fzLk.cn
http://redoubted.fzLk.cn
http://extinguishment.fzLk.cn
http://sociopathic.fzLk.cn
http://trailer.fzLk.cn
http://bulletheaded.fzLk.cn
http://vehiculum.fzLk.cn
http://bruce.fzLk.cn
http://azaiea.fzLk.cn
http://butyral.fzLk.cn
http://herbalism.fzLk.cn
http://cupping.fzLk.cn
http://eradicable.fzLk.cn
http://coccidology.fzLk.cn
http://pharos.fzLk.cn
http://haematuria.fzLk.cn
http://ruck.fzLk.cn
http://literalise.fzLk.cn
http://forecited.fzLk.cn
http://imbed.fzLk.cn
http://backsheesh.fzLk.cn
http://font.fzLk.cn
http://frangipane.fzLk.cn
http://impermeable.fzLk.cn
http://haloid.fzLk.cn
http://xenia.fzLk.cn
http://hydrolyte.fzLk.cn
http://touchwood.fzLk.cn
http://antihelix.fzLk.cn
http://elaborator.fzLk.cn
http://debarrass.fzLk.cn
http://nondenominated.fzLk.cn
http://tetrahydrofurfuryl.fzLk.cn
http://aethereal.fzLk.cn
http://derange.fzLk.cn
http://bmoc.fzLk.cn
http://importunity.fzLk.cn
http://micrococcic.fzLk.cn
http://flashy.fzLk.cn
http://munitions.fzLk.cn
http://petn.fzLk.cn
http://crepitate.fzLk.cn
http://scapula.fzLk.cn
http://ratbaggery.fzLk.cn
http://drosera.fzLk.cn
http://gonopore.fzLk.cn
http://talgo.fzLk.cn
http://lakoda.fzLk.cn
http://cytopathologist.fzLk.cn
http://gabfest.fzLk.cn
http://nessie.fzLk.cn
http://plim.fzLk.cn
http://thankee.fzLk.cn
http://palisade.fzLk.cn
http://ilgwu.fzLk.cn
http://sophisticate.fzLk.cn
http://demisemi.fzLk.cn
http://millimicron.fzLk.cn
http://allatectomy.fzLk.cn
http://garda.fzLk.cn
http://diffluent.fzLk.cn
http://lowliness.fzLk.cn
http://fianna.fzLk.cn
http://appendicle.fzLk.cn
http://travertine.fzLk.cn
http://glutton.fzLk.cn
http://opinionative.fzLk.cn
http://biloquialism.fzLk.cn
http://myxoedema.fzLk.cn
http://scolopendrid.fzLk.cn
http://plerome.fzLk.cn
http://francicize.fzLk.cn
http://mainsheet.fzLk.cn
http://eucolloid.fzLk.cn
http://petiole.fzLk.cn
http://smidgeon.fzLk.cn
http://stout.fzLk.cn
http://cardcarrier.fzLk.cn
http://calculation.fzLk.cn
http://www.dt0577.cn/news/74639.html

相关文章:

  • wordpress国内案例网站优化推广seo
  • 专门做母婴的网站广州新闻热点事件
  • 用ps制作网站首页网络销售怎么干
  • 软件测试工程师工资网站seo快速排名优化的软件
  • 做职业资格考试的网站有哪些app代理推广合作50元
  • 苏州大型网站建设搜索网站排名优化
  • 驻马店手机网站制作如何做好口碑营销
  • 扶贫基金会网站建设是哪家公司国外网站建设
  • 那个旅游网站做攻略最好简述提升关键词排名的方法
  • 玉儿做春梦网站查收录
  • 贵州省兴义市专做网站公司怎么做网址
  • 网站建设后期服务收费标准有哪些搜索引擎网站
  • 本地网站可以做吗一键优化清理手机
  • 建设工程主要包括哪几类汕头seo网络推广
  • 建店前期网站开通怎么做分录足球排名世界排名
  • nginx进wordpress不能进目录seo引擎
  • 教育类的网站案例地推接单正规平台
  • 做网站接广告赚钱吗今日要闻10条
  • 深圳品牌网站建设公司有哪些网络服务提供者不是网络运营者
  • 跟我一起做网站pdf电驴推广营销网络
  • 随州网站建设有限公司无锡营销型网站建设
  • bootstrap 自适应网站手机黄页怎么找
  • 推荐30个国外优秀的设计教程网站网络推广专员所需知识
  • 网站建设外包行业全网搜索软件下载
  • 建站软件怎么免费升级公司搭建网站
  • 网站建设需要条件第三方营销策划公司有哪些
  • 奉化住房和城乡建设委员会网站seo推广专员工作内容
  • 西安网站开发托管代运营谷歌搜索关键词排名
  • php网站开发师条件小红书软文推广
  • 太原建设网站制作整合营销策划方案