Results 1 to 6 of 6

Thread: Model-View: how to get the data from my selected rows

  1. #1
    Join Date
    Jul 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Model-View: how to get the data from my selected rows

    Qt Code:
    1. QModelIndexList listModelIndex = selectionModel->selectedRows();
    2. for (QModelIndexList::Iterator iter = listModelIndex.begin(); iter != listModelIndex.end (); ++iter, ++pChan) {
    3. index = *iter;
    4. row = index.row();
    5. pChan->m_channel = modelChannel->data(modelChannel->index(row, 0), Qt::DisplayRole).toUInt();
    6. pChan->m_isRecorded = modelChannel->data(modelChannel->index(row, 1), Qt::DisplayRole).toUInt();
    7. pChan->m_isPrioritied = modelChannel->data(modelChannel->index(row, 2), Qt::DisplayRole).toUInt();
    8. }
    To copy to clipboard, switch view to plain text mode 

    First I get the list of the selected rows; then I want to get the index of every row. However because the data have been sorted, so the getted row-index in Line L4 is not the one, which I want to get. I want get the index of the selected one in view by my eyes, not the one in the model in computer mem!

    Maybe it is not easy to understand only by my above words. So you can try the example of QT4.4.X\examples\itemviews\addressbook. First you input 3 names and addresses one by one, such as <Alice, anywhere>, <Bring, anywhere> and <John, anywhere>, then you try to change the Alice' adress. You will find it can not to be changed!
    Last edited by jpn; 6th August 2008 at 19:13. Reason: missing [code] tags

  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: Model-View: how to get the data from my selected rows

    I suggest you apply a sort filter proxy model between your model and the view so that the view sorts using the proxy. You'll then have access to mapping between the source and the proxy models so that you can operate on any of them.

  3. #3
    Join Date
    Jul 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Model-View: how to get the data from my selected rows

    Quote Originally Posted by wysota View Post
    I suggest you apply a sort filter proxy model between your model and the view so that the view sorts using the proxy. You'll then have access to mapping between the source and the proxy models so that you can operate on any of them.
    Yes, does define a proxy class derived from QSortFilterProxyModel. But this cannot make it possible to get the wanted index.

    If you try the example, then you can find the real problem!

  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: how to get the data from my selected rows

    Quote Originally Posted by viasant View Post
    But this cannot make it possible to get the wanted index.
    Yes, it does. If it does not, then you are doing something wrong elsewhere.

  5. #5
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Model-View: how to get the data from my selected rows

    Quote Originally Posted by viasant View Post
    Yes, does define a proxy class derived from QSortFilterProxyModel. But this cannot make it possible to get the wanted index.
    Quite simple actually... use mapToSource and mapFromSource. See
    QSortFilterProxyModel documentaion.

    If you have several proxys below each other for sorting on more than one parameter, you need to to mapto/mapfrom starting at the lowest level en go through each proxy to get to your sourcemodel, and then get the index you want!

  6. #6
    Join Date
    Jul 2008
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Model-View: how to get the data from my selected rows

    有没有用过表的关联?

Similar Threads

  1. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 13:14
  2. Table Model / View Problem -- Data Not Displaying
    By jhendersen in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2007, 07:45
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 09:50
  4. Replies: 6
    Last Post: 20th April 2006, 11:23
  5. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 21:59

Tags for this Thread

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.