Results 1 to 2 of 2

Thread: QAbstractTableModel EDITABLE ERASES current value when editable is invoked

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Default QAbstractTableModel EDITABLE ERASES current value when editable is invoked

    I created a TableModel which extends QAbstractTableModel and whenever I double click inside a cell to edit it, if it has a current value, that value gets erased, I've tried overwriting the double click signal and resetting the value, but that didn't help.

    I've included all the files I'm using.

    model.h
    http://pastebin.com/V7VJAuCJ

    model.cpp
    http://pastebin.com/zPjSViFQ

    MainWindow.h
    http://pastebin.com/kGGp06zH

    MainWindow.cpp
    http://pastebin.com/wuRaQ3eE


    Model.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "model.h"
    4. #include <QDebug>
    5.  
    6. /*!
    7.   Constructs a table model with at least one row and one column.
    8. */
    9.  
    10. TableModel::TableModel(int rows, int columns, QObject *parent)
    11. {
    12. QStringList newList;
    13.  
    14. for (int column = 0; column < qMax(1, columns); ++column) {
    15. newList.append("");
    16. }
    17.  
    18. for (int row = 0; row < qMax(1, rows); ++row) {
    19. rowList.append(newList);
    20. }
    21. }
    22.  
    23.  
    24. /*!
    25.   Returns the number of items in the row list as the number of rows
    26.   in the model.
    27. */
    28.  
    29. int TableModel::rowCount(const QModelIndex &/*parent*/) const
    30. {
    31. return rowList.size();
    32. }
    33.  
    34. /*!
    35.   Returns the number of items in the first list item as the number of
    36.   columns in the model. All rows should have the same number of columns.
    37. */
    38.  
    39. int TableModel::columnCount(const QModelIndex &/*parent*/) const
    40. {
    41. return rowList[0].size();
    42. }
    43.  
    44. /*!
    45.   Returns an appropriate value for the requested data.
    46.   If the view requests an invalid index, an invalid variant is returned.
    47.   Any valid index that corresponds to a string in the list causes that
    48.   string to be returned for the display role; otherwise an invalid variant
    49.   is returned.
    50. */
    51.  
    52. QVariant TableModel::data(const QModelIndex &index, int role) const
    53. {
    54. if (!index.isValid())
    55. return QVariant();
    56.  
    57. if (role == Qt::DisplayRole)
    58. return rowList[index.row()][index.column()];
    59. else
    60. return QVariant();
    61. }
    62.  
    63. /*!
    64.   Returns the appropriate header string depending on the orientation of
    65.   the header and the section. If anything other than the display role is
    66.   requested, we return an invalid variant.
    67. */
    68.  
    69. QVariant TableModel::headerData(int section, Qt::Orientation orientation,
    70. int role) const
    71. {
    72. if (role != Qt::DisplayRole)
    73. return QVariant();
    74.  
    75. if (orientation == Qt::Horizontal)
    76. return QString("Column %1").arg(section);
    77. else
    78. return QString("Row %1").arg(section);
    79. }
    80.  
    81. /*!
    82.   Returns an appropriate value for the item's flags. Valid items are
    83.   enabled, selectable, and editable.
    84. */
    85.  
    86. Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
    87. {
    88. if (!index.isValid())
    89. return Qt::ItemIsEnabled;
    90.  
    91. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    92. }
    93.  
    94. /*!
    95.   Changes an item in the model, but only if the following conditions
    96.   are met:
    97.  
    98.   * The index supplied is valid.
    99.   * The role associated with editing text is specified.
    100.  
    101.   The dataChanged() signal is emitted if the item is changed.
    102. */
    103.  
    104. bool TableModel::setData(const QModelIndex &index,
    105. const QVariant &value, int role)
    106. {
    107. if (!index.isValid() || role != Qt::EditRole)
    108. return false;
    109.  
    110. qDebug()<< value.toString();
    111. rowList[index.row()][index.column()] = value.toString();
    112. emit dataChanged(index, index);
    113. return true;
    114. }
    115.  
    116. /*!
    117.   Inserts a number of rows into the model at the specified position.
    118. */
    119.  
    120. bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
    121. {
    122. int columns = columnCount();
    123. beginInsertRows(parent, position, position + rows - 1);
    124.  
    125. for (int row = 0; row < rows; ++row) {
    126. QStringList items;
    127. for (int column = 0; column < columns; ++column)
    128. items.append("");
    129. rowList.insert(position, items);
    130. }
    131.  
    132. endInsertRows();
    133. return true;
    134. }
    135.  
    136. /*!
    137.   Inserts a number of columns into the model at the specified position.
    138.   Each entry in the list is extended in turn with the required number of
    139.   empty strings.
    140. */
    141.  
    142. bool TableModel::insertColumns(int position, int columns, const QModelIndex &parent)
    143. {
    144. int rows = rowCount();
    145. beginInsertColumns(parent, position, position + columns - 1);
    146.  
    147. for (int row = 0; row < rows; ++row) {
    148. for (int column = position; column < columns; ++column) {
    149. rowList[row].insert(position, "");
    150. }
    151. }
    152.  
    153. endInsertColumns();
    154. return true;
    155. }
    156.  
    157. /*!
    158.   Removes a number of rows from the model at the specified position.
    159. */
    160.  
    161. bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
    162. {
    163. beginRemoveRows(parent, position, position + rows - 1);
    164.  
    165. for (int row = 0; row < rows; ++row) {
    166. rowList.removeAt(position);
    167. }
    168.  
    169. endRemoveRows();
    170. return true;
    171. }
    172.  
    173. /*!
    174.   Removes a number of columns from the model at the specified position.
    175.   Each row is shortened by the number of columns specified.
    176. */
    177.  
    178. bool TableModel::removeColumns(int position, int columns, const QModelIndex &parent)
    179. {
    180. int rows = rowCount();
    181. beginRemoveColumns(parent, position, position + columns - 1);
    182.  
    183. for (int row = 0; row < rows; ++row) {
    184. for (int column = 0; column < columns; ++column) {
    185. rowList[row].removeAt(position);
    186. }
    187. }
    188.  
    189. endRemoveColumns();
    190. return true;
    191. }
    To copy to clipboard, switch view to plain text mode 



    model.h

    Qt Code:
    1. #ifndef MODEL_H
    2. #define MODEL_H
    3.  
    4. #include <QAbstractTableModel>
    5. #include <QStringList>
    6. #include <QVariant>
    7.  
    8. class TableModel : public QAbstractTableModel
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. TableModel(int rows = 1, int columns = 1, QObject *parent = 0);
    14.  
    15. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    16. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    17. QVariant data(const QModelIndex &index, int role) const;
    18. QVariant headerData(int section, Qt::Orientation orientation,
    19. int role = Qt::DisplayRole) const;
    20.  
    21. Qt::ItemFlags flags(const QModelIndex &index) const;
    22. bool setData(const QModelIndex &index, const QVariant &value,
    23. int role = Qt::EditRole);
    24.  
    25. bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex());
    26. bool insertColumns(int position, int columns, const QModelIndex &parent = QModelIndex());
    27. bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex());
    28. bool removeColumns(int position, int columns, const QModelIndex &parent = QModelIndex());
    29.  
    30. private:
    31. QList<QStringList> rowList;
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QAbstractTableModel EDITABLE ERASES current value when editable is invoked

    Your model data() function returns a null QVariant for the EditRole and that is the value that Qt gives to the default editor.

Similar Threads

  1. Editable QLCDNumber
    By QDmitry in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2013, 15:57
  2. Column Not Editable
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 02:43
  3. QGraphicsTextItem is not editable
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 29th September 2009, 16:51
  4. how to set up a QHeaderView editable?
    By iguanna in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2008, 15:37
  5. QTreeWidget editable
    By hgedek in forum Newbie
    Replies: 2
    Last Post: 5th February 2008, 09:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.