Results 1 to 4 of 4

Thread: [ Solved ] a QTableView/QAbstractTableModel problem, please help

  1. #1
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    3
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [ Solved ] a QTableView/QAbstractTableModel problem, please help

    I used a QTableView and an editable QAbstractTableModel subclass to display a large dataset.
    But when I double click an item, the original data of the item is gone,
    how can I edit the orginal data.
    thanks.

    model.h
    Qt Code:
    1. class Model : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Model(QObject *parent = 0);
    7.  
    8. void setSourceData(const QString &fileName); // to get data
    9.  
    10. int rowCount(const QModelIndex &parent) const;
    11. int columnCount(const QModelIndex &parent) const;
    12. QVariant data(const QModelIndex &index, int role) const;
    13. bool setData(const QModelIndex &index, const QVariant &value,
    14. int role);
    15. QVariant headerData(int section, Qt::Orientation orientation,
    16. int role) const;
    17. Qt::ItemFlags flags(const QModelIndex &index) const;
    18.  
    19. private:
    20. QStringList rowNames;
    21. QStringList columnNames;
    22. QVector<QVector<int> > dataset;
    23. };
    To copy to clipboard, switch view to plain text mode 

    model.cpp(fragment)
    Qt Code:
    1. void Model::setSourceData(const QString &fileName)(const QString &fileName)
    2. {
    3. // read in data using QTextStream
    4. }
    5.  
    6. QVariant Model::data(const QModelIndex &index, int role) const
    7. {
    8. if (!index.isValid())
    9. return QVariant();
    10.  
    11. if (role == Qt::TextAlignmentRole)
    12. {
    13. return int(Qt::AlignRight | Qt::AlignVCenter);
    14. }
    15. else if (role == Qt::DisplayRole)
    16. {
    17. return dataset[index.row()][index.column()];
    18. }
    19. // problem solved, I forgot the Qt::EditRole
    20. return QVariant();
    21. }
    22.  
    23. bool Model::setData(const QModelIndex &index,
    24. const QVariant &value, int role)
    25. {
    26. if (index.isValid() && role == Qt::EditRole)
    27. {
    28. if (value.toString().isEmpty()) return false;
    29. dataset[index.row()][index.column()] = value.toInt();
    30. QModelIndex transposedIndex = createIndex(index.column(),
    31. index.row());
    32. emit dataChanged(index, index);
    33. emit dataChanged(transposedIndex, transposedIndex);
    34. return true;
    35. }
    36. return false;
    37. }
    38.  
    39. Qt::ItemFlags CityModel::flags(const QModelIndex &index) const
    40. {
    41. Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    42. flags |= (Qt::ItemIsEditable
    43. |Qt::ItemIsSelectable
    44. |Qt::ItemIsUserCheckable
    45. |Qt::ItemIsEnabled
    46. |Qt::ItemIsDragEnabled
    47. |Qt::ItemIsDropEnabled);
    48. return flags;
    49. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp(fragment)
    Qt Code:
    1. Model model;
    2. model.setSourceData("data.txt");
    3. QTableView tableView;
    4. tableView.setModel(&model);
    To copy to clipboard, switch view to plain text mode 
    Last edited by lvdong; 23rd November 2010 at 12:36.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a QTableView/QAbstractTableModel problem, please help

    What does your model's data() function return for Qt::EditRole?

    Also, please use the code tags for chunks of code.
    Last edited by franz; 23rd November 2010 at 12:08. Reason: Added code tag request
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following user says thank you to franz for this useful post:

    lvdong (23rd November 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    3
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a QTableView/QAbstractTableModel problem, please help

    thanks, I forgot the EditRole.

  5. #4
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a QTableView/QAbstractTableModel problem, please help

    3q for your help! I do as you do!

    QVariant Model::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    if (role == Qt::TextAlignmentRole)
    {
    return int(Qt::AlignRight | Qt::AlignVCenter);
    }
    else if (role == Qt:isplayRole)
    {
    // problem solved, I forgot the Qt::EditRole
    return dataset[index.row()][index.column()];
    }
    else if(role == Qt::EditRole)
    {
    QString str = dataset[index.row()][index.column()];
    return str;
    }

    return QVariant();
    }

Similar Threads

  1. qabstracttablemodel/qtableview: vector data columns
    By xyfix in forum Qt Programming
    Replies: 0
    Last Post: 15th September 2010, 12:51
  2. Replies: 4
    Last Post: 6th May 2009, 08:18
  3. Performance with QTableView and QAbstractTableModel
    By MBex in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2009, 08:04
  4. Replies: 3
    Last Post: 29th January 2009, 08:38
  5. QAbstractTableModel for QTreeView?
    By Michiel in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2007, 09:09

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.