examples/tutorials/modelview/5_edit/mymodel.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    37 ** $QT_END_LICENSE$
    37 ** $QT_END_LICENSE$
    38 **
    38 **
    39 ****************************************************************************/
    39 ****************************************************************************/
    40 
    40 
    41 //! [quoting mymodel_d]
    41 
    42 #include "mymodel.h"
    42 #include "mymodel.h"
    43 
    43 
    44 const int COLS= 3;
       
    45 const int ROWS= 2;
       
    46 
    44 
    47 MyModel::MyModel(QObject *parent)
    45 MyModel::MyModel(QObject *parent)
    48     :QAbstractTableModel(parent)
    46     :QAbstractTableModel(parent)
    49 {
    47 {
    50     //gridData needs to have 6 element, one for each table cell
       
    51     m_gridData << "1/1" << "1/2" << "1/3" << "2/1" << "2/2" << "2/3" ;
       
    52 }
    48 }
    53 //! [quoting mymodel_d]
       
    54 
    49 
    55 //! [quoting mymodel_e]
    50 //-----------------------------------------------------------------
    56 int MyModel::rowCount(const QModelIndex & /*parent*/) const
    51 int MyModel::rowCount(const QModelIndex & /*parent*/) const
    57 {
    52 {
    58     return ROWS;
    53     return ROWS;
    59 }
    54 }
    60 
    55 
       
    56 //-----------------------------------------------------------------
    61 int MyModel::columnCount(const QModelIndex & /*parent*/) const
    57 int MyModel::columnCount(const QModelIndex & /*parent*/) const
    62 {
    58 {
    63     return COLS;
    59     return COLS;
    64 }
    60 }
    65 
    61 
       
    62 //-----------------------------------------------------------------
    66 QVariant MyModel::data(const QModelIndex &index, int role) const
    63 QVariant MyModel::data(const QModelIndex &index, int role) const
    67 {
    64 {
    68     if (role == Qt::DisplayRole)
    65     if (role == Qt::DisplayRole)
    69     {
    66     {
    70         return m_gridData[modelIndexToOffset(index)];
    67         return m_gridData[index.row()][index.column()];
    71     }
    68     }
    72     return QVariant();
    69     return QVariant();
       
    70 }
       
    71 
       
    72 //-----------------------------------------------------------------
       
    73 //! [quoting mymodel_e]
       
    74 bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
       
    75 {
       
    76     if (role == Qt::EditRole)
       
    77     {
       
    78         //save value from editor to member m_gridData
       
    79         m_gridData[index.row()][index.column()] = value.toString();
       
    80 	//for presentation purposes only: build and emit a joined string
       
    81 	QString result; 
       
    82 	for(int row= 0; row < ROWS; row++)
       
    83 	{
       
    84             for(int col= 0; col < COLS; col++)
       
    85 	    {
       
    86 	        result += m_gridData[row][col] + " ";
       
    87 	    }
       
    88         }
       
    89         emit editCompleted( result );
       
    90     }
       
    91     return true;
    73 }
    92 }
    74 //! [quoting mymodel_e]
    93 //! [quoting mymodel_e]
    75 
    94 
    76 //-----------------------------------------------------------------
    95 //-----------------------------------------------------------------
    77 
       
    78 //! [quoting mymodel_f]
    96 //! [quoting mymodel_f]
    79 bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
       
    80 {
       
    81     if (role == Qt::EditRole)
       
    82     {
       
    83         m_gridData[modelIndexToOffset(index)] = value.toString();
       
    84         emit editCompleted(m_gridData.join(" | "));
       
    85     }
       
    86     return true;
       
    87 }
       
    88 
       
    89 Qt::ItemFlags MyModel::flags(const QModelIndex & /*index*/) const
    97 Qt::ItemFlags MyModel::flags(const QModelIndex & /*index*/) const
    90 {
    98 {
    91     return Qt::ItemIsSelectable |  Qt::ItemIsEditable | Qt::ItemIsEnabled ;
    99     return Qt::ItemIsSelectable |  Qt::ItemIsEditable | Qt::ItemIsEnabled ;
    92 }
   100 }
    93 
       
    94 //convert row and column information to array offset
       
    95 int MyModel::modelIndexToOffset(const QModelIndex & index) const
       
    96 {
       
    97     return index.row()*COLS + index.column();
       
    98 }
       
    99 //! [quoting mymodel_f]
   101 //! [quoting mymodel_f]