Results 1 to 3 of 3

Thread: sort row by column , when getting text alwas wrong

  1. #1
    Join Date
    May 2009
    Posts
    83

    Default sort row by column , when getting text alwas wrong

    hi
    im trying to get text and data from column in index number 0 from row that is selected
    but i never get the right data im using simple model view treeview with QSortFilterProxyModel proxy to sort the columns and QStandardItemModel as the model

    this is the slot function that is triggered on each doubleClicked

    Qt Code:
    1. connect(ui.treeView_mainwindow, SIGNAL(doubleClicked( const QModelIndex &)), this,SLOT(tree_itemClicked( const QModelIndex &)));
    2. ....
    3. ...
    4. void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
    5. {
    6. int iSelectedRow = index.row();
    7. QString groupID;
    8. QString groupName;
    9. groupID = m_model->item(iSelectedRow,0)->data(Qt::UserRole).toString();
    10. groupName = m_model->item(iSelectedRow,0)->text();
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: sort row by column , when getting text alwas wrong

    Get the data / text from the model, to which the item belongs to, like this
    Qt Code:
    1. void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
    2. {
    3. QString groupID = index.model()->data(index, Qt::UserRole).toString();
    4. QString groupName = index.model()->data(index, Qt::DataRole).toString();
    5.  
    6. //or
    7. QString groupID = index.data(Qt::UserRole).toString();
    8. QString groupName = index.data(Qt::DataRole).toString();
    9. }
    To copy to clipboard, switch view to plain text mode 

    If you want the data from column 0, then create index for column 0 then get the data for it, like this
    Qt Code:
    1. void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
    2. {
    3. QString groupID = index.model()->data(index.model()->index(index.row(), 0, index.parent()), Qt::UserRole).toString();
    4. QString groupName = index.model()->data(index.model()->index(index.row(), 0, index.parent()), Qt::DataRole).toString();
    5.  
    6. //or
    7. QString groupID = index.model()->index(index.row(), 0, index.parent()).data(Qt::UserRole).toString();
    8. QString groupName = index.model()->index(index.row(), 0, index.parent()).data(Qt::DataRole).toString();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 5th July 2011 at 02:02.

  3. #3
    Join Date
    May 2009
    Posts
    83

    Default Re: sort row by column , when getting text alwas wrong

    Hello
    Thanks for the response its working great , i have one more question
    how can i set data to colmn in index ( for example ) 3 in the selected row?

    this dosn't work :
    Qt Code:
    1. index.model()->index(index.row(), 3, index.parent()).setData(.......)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 15th June 2010, 14:22
  2. How does the QTableWidget sort a column?
    By codemonkey in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2009, 13:21
  3. wrong sort in qtableview
    By toem in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2009, 10:30
  4. Replies: 2
    Last Post: 20th August 2008, 15:55
  5. QTreeWidget Single Column Sort Disable
    By craigjameshamilton in forum Newbie
    Replies: 1
    Last Post: 21st April 2008, 08:08

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.