zoukankan      html  css  js  c++  java
  • qt mvc2

    继续上次的例子,对于list才说只有行,讨论列是没有意义的。

    bool insertRows(int row, int count, const QModelIndex &parent);
    bool removeRows(int row, int count, const QModelIndex &parent);

    在文档中,insertRows是这么写的,在支持这个操作的models中,在给定的row之前插入count数目的row到model中,插入的都是parent的孩子。

    如果row=0,这些行被插入到parent存在的行,如果row=rowCount(),也是如此。如果parent无孩子,这些rows作为一个单列被插入。在最后明确强调了必须使用beginInsertRows和endInsertRows,所以在实现这个的时候,这两个函数才是关键,看这两个函数,这两个在QAbstractItemModel虚基类中是protect,所以他们只是工具函数。

    首先beginInsertRows

    void beginInsertRows(const QModelIndex &parent, int first, int last);参数first是rows开始处,last是结束处,所以是插入长度是last-first+1,这两个参数是有insertRows的row和count参数决定。

    可是实际时,我们在插入行时,只是可以在原有的数据之后添加一些数据,所以first=现在的行数(行数从0开始,所以现在的行数就是新的行),last=first+count-1。

    在我的例子代码中
    [cpp] view plaincopyprint?
    <span style="font-size:18px;">bool StringListModel::insertRows(int row, int count, const QModelIndex &/*parent*/)
    {
    beginInsertRows(QModelIndex(),row,row + count -1);
    for (int i = 0;i < count;++i) {
    //链表插入的时候,row大于size()时按size()来算
    m_slist.insert(row,QString());
    }
    endInsertRows();
    return true;
    }</span>

    addmodel直接修改模型
    [cpp] view plaincopyprint?
    <span style="font-size:18px;">void StringListModel::addmodel(const QString &c)
    {
    insertRows(rowCount(QModelIndex()),1,QModelIndex());
    m_slist.replace(rowCount(QModelIndex())-1,c);
    }</span>

    现在insertRow的两个参数可以很好的理解了。

    也可以这么来
    [cpp] view plaincopyprint?
    <span style="font-size:18px;">void StringListModel::addmodel(const QString &c)
    {
    // insertRows(rowCount(QModelIndex()),1,QModelIndex());
    insertRow(rowCount(QModelIndex()));
    m_slist.replace(rowCount(QModelIndex())-1,c);
    }</span>

  • 相关阅读:
    Gson字符串编码,字符串转换成图片保存,二进制转换成图片保存
    时间工具类DateUtil
    RSA 签名、验证、加密、解密帮助类
    富友数据加密解密
    Base64(2)
    MD5(2)
    实体对象操作工具
    HttpClient工具类
    MD5
    身份证算法实现
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/3736209.html
Copyright © 2011-2022 走看看