您当前的位置: 首页 >  qt

txwtech

暂无认证

  • 3浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

QT字符串列表模型-编辑功能-插入与删除行的操作

txwtech 发布时间:2022-10-05 17:02:57 ,浏览量:3

QT字符串列表模型-编辑功能-插入与删除行的操作
QAbstractListModel,StringListModel

pro文件添加:

QT+=widgets

HEADERS += \
    stringlistmodel.h

SOURCES += \
    main.cpp \
    stringlistmodel.cpp
#include "stringlistmodel.h"
#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H
#include 
#include 


class StringListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    StringListModel(const QStringList &strings,QObject *parent =0):
        QAbstractListModel(parent),stringList(strings){}
        int rowCount(const QModelIndex &parent=QModelIndex()) const;
        QVariant data(const QModelIndex &index,int role) const;
        QVariant headerData(int section,Qt::Orientation orientation,
                            int role=Qt::DisplayRole) const;
        //添加编辑功能
        Qt::ItemFlags flags(const QModelIndex &index) const;
        bool setData(const QModelIndex &index,const QVariant &value,int role=Qt::EditRole);

        //设置插入与删除行
        bool InsertRows(int position,int rows,const QModelIndex &index=QModelIndex());
        bool RemoveRows(int position,int rows,const QModelIndex &index=QModelIndex());

private:
    QStringList stringList;
};


#endif // STRINGLISTMODEL_H

stringlistmodel.cpp

#include "stringlistmodel.h"
//创建只读模型。。。by txwtech

//StringListModel::StringListModel()
//{

//}

int StringListModel::rowCount(const QModelIndex &parent) const
{
    return stringList.count();
}

QVariant StringListModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
    {
        return QVariant();
    }
    if(index.row()==stringList.count())
    {
        return QVariant();
    }
  //  if(role==Qt::DisplayRole)//只读模型
    if(role==Qt::DisplayRole||role==Qt::EditRole)//编辑功能
    {
        return stringList.at(index.row());
    }
    else
    {
        return QVariant();
    }
}

QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role!=Qt::DisplayRole)
        return QVariant();
    if(orientation==Qt::Horizontal)
        return QString("Column is %1").arg(section);
    else
        return QString("Row is %1").arg(section);
}

Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
    {
        return Qt::ItemIsEnabled;

    }
    return QAbstractItemModel::flags(index)|Qt::ItemIsEditable;//设置可编辑模式的功能
}

bool StringListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(index.isValid()&&role==Qt::EditRole)
    {
        stringList.replace(index.row(),value.toString());
        emit dataChanged(index,index);//发射数据有改变的信号
        return true;
    }
    return false;
}

bool StringListModel::InsertRows(int position, int rows, const QModelIndex &index)
{
    beginInsertRows(QModelIndex(),position,position+rows-1);
    for(int row=0;row            
关注
打赏
1665060526
查看更多评论
0.1388s