PDA

View Full Version : highlight a selected row in a QTreeView without clicking on it



cydside
7th July 2009, 23:33
Hi to all,
I'm using a QTreeView to list items, I hide the QTreeView after a row selection and then show it setting correctly the current index but I don't know how to highlight the row. The following it has been my attempt (rows 18-19):



void frmTitoli::funModifica()
{
QModelIndexList selection = ui.tvView->selectionModel()->selectedRows(0);

if (!selection.empty())
{
QModelIndex idIndex = selection.at(0);
int id = idIndex.data().toInt();

ui.tvView->hide();

...

ui.tvView->show();

refreshView();

ui.tvView->setFocus();
ui.tvView->selectionModel()->setCurrentIndex(idIndex, QItemSelectionModel::SelectCurrent);
}
else
{
QMessageBox::warning(this, tr("Attenzione!"),
tr("Selezionare prima un elemento nella lista!"),
QMessageBox::Ok);
}
}


Thanks!!!

codeslicer
7th July 2009, 23:55
Have you seen QItemSelectionModel::select()?

You can access the selection model with QAbstractItemView::selectionModel()

cydside
8th July 2009, 00:01
Yes, I've tried:



void frmTitoli::funModifica()
{
QModelIndexList selection = ui.tvView->selectionModel()->selectedRows(0);
QItemSelection rowSel = ui.tvView->selectionModel()->selection();

if (!selection.empty())
{
QModelIndex idIndex = selection.at(0);
int id = idIndex.data().toInt();

ui.tvView->hide();

...

ui.tvView->show();

refreshView();

ui.tvView->setFocus();
ui.tvView->selectionModel()->setCurrentIndex(idIndex, QItemSelectionModel::SelectCurrent);
ui.tvView->selectionModel()->select(rowSel, QItemSelectionModel::SelectCurrent);
}
else
{
QMessageBox::warning(this, tr("Attenzione!"),
tr("Selezionare prima un elemento nella lista!"),
QMessageBox::Ok);
}
}

but it doesn't work :(

jeffpogo
18th November 2009, 00:01
Did you ever get this working?

How did you do it?

Thanks.