PDA

View Full Version : sort row by column , when getting text alwas wrong



umen
4th July 2011, 15:10
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



connect(ui.treeView_mainwindow, SIGNAL(doubleClicked( const QModelIndex &)), this,SLOT(tree_itemClicked( const QModelIndex &)));
....
...
void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
{
int iSelectedRow = index.row();
QString groupID;
QString groupName;
groupID = m_model->item(iSelectedRow,0)->data(Qt::UserRole).toString();
groupName = m_model->item(iSelectedRow,0)->text();

}

Santosh Reddy
5th July 2011, 02:52
Get the data / text from the model, to which the item belongs to, like this :)

void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
{
QString groupID = index.model()->data(index, Qt::UserRole).toString();
QString groupName = index.model()->data(index, Qt::DataRole).toString();

//or
QString groupID = index.data(Qt::UserRole).toString();
QString groupName = index.data(Qt::DataRole).toString();
}


If you want the data from column 0, then create index for column 0 then get the data for it, like this

void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
{
QString groupID = index.model()->data(index.model()->index(index.row(), 0, index.parent()), Qt::UserRole).toString();
QString groupName = index.model()->data(index.model()->index(index.row(), 0, index.parent()), Qt::DataRole).toString();

//or
QString groupID = index.model()->index(index.row(), 0, index.parent()).data(Qt::UserRole).toString();
QString groupName = index.model()->index(index.row(), 0, index.parent()).data(Qt::DataRole).toString();
}

umen
5th July 2011, 09:35
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 :

index.model()->index(index.row(), 3, index.parent()).setData(.......)