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
Code:
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();
groupID = m_model->item(iSelectedRow,0)->data(Qt::UserRole).toString();
groupName = m_model->item(iSelectedRow,0)->text();
}
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 :)
Code:
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
Code:
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();
}
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 :
Code:
index.model()->index(index.row(), 3, index.parent()).setData(.......)