⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringlistmodel.cpp

📁 Linux窗口程序设计,Qt4精彩实例分析,以循序渐进的方式介绍Qt4开发及其实例子,第一部分
💻 CPP
字号:
#include "stringlistmodel.h"

StringListModel::StringListModel(const QStringList &strings,QObject *parent)
	: QAbstractListModel(parent),stringList(strings)
{}

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.size())
         return QVariant();

     if (role == Qt::DisplayRole)
         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 %1").arg(section);
     else
         return QString("Row %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 < rows; ++row) 
     {
         stringList.insert(position, "");
     }

     endInsertRows();
     return true;
}

bool StringListModel::removeRows(int position, int rows, const QModelIndex &index)
{
     beginRemoveRows(QModelIndex(), position, position+rows-1);

     for (int row = 0; row < rows; ++row) 
     {
         stringList.removeAt(position);
     }

     endRemoveRows();
     return true;
}
                                       

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -