Results 1 to 4 of 4

Thread: Model/View problem in move from 4.2.3 to 4.3.0

  1. #1
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Model/View problem in move from 4.2.3 to 4.3.0

    I'm having a problem with code that worked in 4.2.3 not working in 4.3.0. I've pared it down to a simple test case.

    The model inherits QAbstractTableModel. relevant code:

    Qt Code:
    1. Qt::ItemFlags TestModel::flags(const QModelIndex &index) const
    2. {
    3. if (index.row() == column1_.count() && index.column() != 0)
    4. return QAbstractTableModel::flags(index);
    5. else
    6. return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
    7. }
    8.  
    9. int TestModel::rowCount(const QModelIndex&) const
    10. {
    11. return column1_.count() + 1;
    12. }
    13. QVariant TestModel::data(const QModelIndex& index, int role) const
    14. {
    15. if (!index.isValid() || role != Qt::DisplayRole)
    16. return QVariant();
    17. else if (index.row() == column1_.size())
    18. return QVariant();
    19. else if (index.column() == 0)
    20. return column1_.at(index.row());
    21. else
    22. return column2_.at(index.row());
    23. }
    24.  
    25. bool TestModel::setData(const QModelIndex &index, const QVariant &value, int role)
    26. {
    27. if (!index.isValid() || role != Qt::EditRole)
    28. return false;
    29. if (index.row() == column1_.size())
    30. {
    31. if (index.column() != 0)
    32. return false;
    33. else
    34. {
    35. column1_ << value.toString();
    36. column2_ << QString();
    37. emit layoutChanged();
    38. return true;
    39. }
    40. }
    41. else
    42. {
    43. if (index.column() == 0)
    44. column1_[index.row()] = value.toString();
    45. else
    46. column2_[index.row()] = value.toString();
    47.  
    48. emit dataChanged(index, index);
    49. return true;
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    I'm using QTableViewfor the view.

    In 4.2.3, when I enter data in the last row in column 1, a new row immediately appears immediately below the last row (see rowCount function). In 4.3.0, no new row appears, event though I'm emitting layoutChanged(). Is there something I'm missing and 4.2.3 let me get by?

    Any help would be very much appreciated.
    Jim L
    Seattle, WA

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View problem in move from 4.2.3 to 4.3.0

    It shouldn't work with 4.2.3 either, because you don't invoke beginInsertRows() / endInsertRows() when you add a new row.

    It should be:
    Qt Code:
    1. else
    2. {
    3. beginInsertRows( QModelIndex(), index.row(), index.row() );
    4. column1_ << value.toString();
    5. column2_ << QString();
    6. endInsertRows();
    7. return true;
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Model/View problem in move from 4.2.3 to 4.3.0

    Thanks! That seems to do the trick. The documentation seems to say to call beginInsertRows() before insertRows() (plural) - not for a single row. Since it worked with Qt 4.2, I assumed that interpretation was correct.
    Jim L
    Seattle, WA

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View problem in move from 4.2.3 to 4.3.0

    insertRow() calls insertRows() to do its job and insertRows() should call beginInsertRows() hence when inserting a single row (using insertRow()) beginInsertRows() is also called.

    See the documentation of beginInsertRows() for more details.

  5. The following user says thank you to wysota for this useful post:

    jml (1st November 2007)

Similar Threads

  1. QGraphicsView scrolling problem with 4.3.0
    By hb in forum Qt Programming
    Replies: 8
    Last Post: 30th August 2007, 22:18

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.