Results 1 to 4 of 4

Thread: QAbstractModel insert row at the top, keep current item selected

  1. #1
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QAbstractModel insert row at the top, keep current item selected

    I have a very simple QAbstractModel for a flat QTreeView (no children).

    I use an external data structure to return a value for the data() method, shown below.

    Qt Code:
    1. QVariant MyModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. if (role != Qt::DisplayRole && role != Qt::EditRole)
    7. return QVariant();
    8.  
    9. if (index.column() == 0)
    10. return m_data->getSize() - index.row();
    11.  
    12. return QVariant();
    13. }
    To copy to clipboard, switch view to plain text mode 

    This just simply provides a reverse ordered list for the first column, with the newest numbers arriving on top. AKA --

    4
    3
    2 <-- User has this selected
    1

    So, when new data arrives, my m_data vector is incremented (outside of the model), and I call layoutChanged() on the model.

    The issue is that I want the user to be able to select a particular item, say #2, and have that selection stay on #2, even when a 5th element is added to the top.

    What happens is that the selection doesn't follow the item, but rather stays in the same position -- presumably because the items aren't being "inserted" correctly.

    5
    4
    3 <-- Selection stays here: in the same list position when a 5th item is added
    2 <-- I want the selection to remain with the number that was previously selected
    1

    I've tried various combinations of beginInsertRows(), endInsertRows(), layoutAboutToBeChanged(), to no avail.

    I've even thought of trying to store the current selection and replace it after adding data, though I suspect this will fire some selection events that I'll need to filter.

    Is there an easy way to handle this without embedding the data structure inside the model?

  2. #2
    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: QAbstractModel insert row at the top, keep current item selected

    You should use beginInsertRow() followed by endInsertRows() followed by dataChanged() for the first column (since data in all rows changes).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractModel insert row at the top, keep current item selected

    Quote Originally Posted by wysota View Post
    You should use beginInsertRow() followed by endInsertRows() followed by dataChanged() for the first column (since data in all rows changes).
    Shouldn't the begin/end insert rows duo be enough?
    The value of the other rows stay the same, no?

    Something like
    Qt Code:
    1. // insert row, top level, at row 0
    2. beginInsertRows(QModelIndex(), 0, 0);
    3.  
    4. //prepend row to m_data
    5.  
    6. endInsertRows();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    alketi (1st December 2014)

  5. #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: QAbstractModel insert row at the top, keep current item selected

    Ah, right, of course.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    alketi (1st December 2014)

Similar Threads

  1. Replies: 3
    Last Post: 3rd January 2012, 23:44
  2. Replies: 0
    Last Post: 10th March 2011, 11:44
  3. Disable menu item based to current control selected
    By Suppaman in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2010, 07:33
  4. Changing selected item color in non-current window.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2007, 07:09
  5. How to know the current tab selected
    By Mrdata in forum Newbie
    Replies: 11
    Last Post: 6th February 2007, 19:46

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.