examples/itemviews/addressbook/tablemodel.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    70 //! [2]
    70 //! [2]
    71 QVariant TableModel::data(const QModelIndex &index, int role) const
    71 QVariant TableModel::data(const QModelIndex &index, int role) const
    72 {
    72 {
    73     if (!index.isValid())
    73     if (!index.isValid())
    74         return QVariant();
    74         return QVariant();
    75     
    75 
    76     if (index.row() >= listOfPairs.size() || index.row() < 0)
    76     if (index.row() >= listOfPairs.size() || index.row() < 0)
    77         return QVariant();
    77         return QVariant();
    78     
    78 
    79     if (role == Qt::DisplayRole) {
    79     if (role == Qt::DisplayRole) {
    80         QPair<QString, QString> pair = listOfPairs.at(index.row());
    80         QPair<QString, QString> pair = listOfPairs.at(index.row());
    81         
    81 
    82         if (index.column() == 0)
    82         if (index.column() == 0)
    83             return pair.first;
    83             return pair.first;
    84         else if (index.column() == 1)
    84         else if (index.column() == 1)
    85             return pair.second;
    85             return pair.second;
    86     }
    86     }
    91 //! [3]
    91 //! [3]
    92 QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
    92 QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
    93 {
    93 {
    94     if (role != Qt::DisplayRole)
    94     if (role != Qt::DisplayRole)
    95         return QVariant();
    95         return QVariant();
    96     
    96 
    97     if (orientation == Qt::Horizontal) {
    97     if (orientation == Qt::Horizontal) {
    98         switch (section) {
    98         switch (section) {
    99             case 0:
    99             case 0:
   100                 return tr("Name");
   100                 return tr("Name");
   101                 
   101 
   102             case 1:
   102             case 1:
   103                 return tr("Address");
   103                 return tr("Address");
   104                 
   104 
   105             default:
   105             default:
   106                 return QVariant();
   106                 return QVariant();
   107         }
   107         }
   108     }
   108     }
   109     return QVariant();
   109     return QVariant();
   113 //! [4]
   113 //! [4]
   114 bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
   114 bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
   115 {
   115 {
   116     Q_UNUSED(index);
   116     Q_UNUSED(index);
   117     beginInsertRows(QModelIndex(), position, position+rows-1);
   117     beginInsertRows(QModelIndex(), position, position+rows-1);
   118     
   118 
   119     for (int row=0; row < rows; row++) {
   119     for (int row=0; row < rows; row++) {
   120         QPair<QString, QString> pair(" ", " ");
   120         QPair<QString, QString> pair(" ", " ");
   121         listOfPairs.insert(position, pair);
   121         listOfPairs.insert(position, pair);
   122     }
   122     }
   123 
   123 
   127 //! [4]
   127 //! [4]
   128 
   128 
   129 //! [5]
   129 //! [5]
   130 bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
   130 bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
   131 {
   131 {
   132     Q_UNUSED(index);    
   132     Q_UNUSED(index);
   133     beginRemoveRows(QModelIndex(), position, position+rows-1);
   133     beginRemoveRows(QModelIndex(), position, position+rows-1);
   134     
   134 
   135     for (int row=0; row < rows; ++row) {
   135     for (int row=0; row < rows; ++row) {
   136         listOfPairs.removeAt(position);
   136         listOfPairs.removeAt(position);
   137     }
   137     }
   138 
   138 
   139     endRemoveRows();
   139     endRemoveRows();
   142 //! [5]
   142 //! [5]
   143 
   143 
   144 //! [6]
   144 //! [6]
   145 bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
   145 bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
   146 {
   146 {
   147 	if (index.isValid() && role == Qt::EditRole) {
   147     if (index.isValid() && role == Qt::EditRole) {
   148 		int row = index.row();
   148         int row = index.row();
   149 				
   149 
   150 		QPair<QString, QString> p = listOfPairs.value(row);
   150         QPair<QString, QString> p = listOfPairs.value(row);
   151 		
   151 
   152 		if (index.column() == 0)
   152         if (index.column() == 0)
   153 			p.first = value.toString();
   153             p.first = value.toString();
   154 		else if (index.column() == 1)
   154         else if (index.column() == 1)
   155 			p.second = value.toString();
   155             p.second = value.toString();
   156         else
   156         else
   157             return false;
   157             return false;
   158             
   158 
   159         listOfPairs.replace(row, p);
   159         listOfPairs.replace(row, p);
   160 		emit(dataChanged(index, index));
   160         emit(dataChanged(index, index));
   161 		
   161 
   162         return true;
   162         return true;
   163 	}
   163     }
   164 
   164 
   165 	return false;
   165     return false;
   166 }
   166 }
   167 //! [6]
   167 //! [6]
   168 
   168 
   169 //! [7]
   169 //! [7]
   170 Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
   170 Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
   171 {
   171 {
   172     if (!index.isValid())
   172     if (!index.isValid())
   173         return Qt::ItemIsEnabled;
   173         return Qt::ItemIsEnabled;
   174     
   174 
   175     return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
   175     return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
   176 }
   176 }
   177 //! [7]
   177 //! [7]
   178 
   178 
   179 //! [8]
   179 //! [8]