PDA

View Full Version : Problems with getting QTreeView selected Index?



mut
27th June 2014, 17:37
Hello;

I'm writing Qt widget application. I'm having a QTreeView located on a QTabWidget, this QTabWidget itself located on my QMainWindow.

This following code does not give me the correct selected index when called from QMainWindow method:

QModelIndex CurrentIndex = ui->MyTreeView->currentIndex();

Which one of the four followings approach should I use:

1)
QModelIndexList indexes = ui->MyTreeView->selectionModel()->selection().indexes();

// then capture the particular index in a loop

2)
QModelIndex currentIndex = ui->MTreeView->selectionModel()->currentIndex();

3)
QModelIndex CurrentIndex = ui->MreeView->currentIndex();

4)
void Parent::slot_MyTreeView_selectionChanged()
{
m_SelectedIndex= ui->MyTreeView->currentIndex();
}

Which one is the best?

Is my problem a focus problem?

Should I capture QTreeView selected index only when the QTreeView is visible and my mouse is on it (I'm asking this because sometimes the problematic code works fine)?

anda_skoa
28th June 2014, 11:38
The current selection does not necessarily have to be the current index.

Current index can always only be one, while a view can have a lot of indexes selected.

So it depends on what you want to achieve. The current index is the one with "keyboard focus".

Cheers,
_

mut
28th June 2014, 22:03
Thank you for the reply. I want to have the index of the first item (column 0) of a row when the row is selected by the user.

anda_skoa
29th June 2014, 13:00
That sounds like a use case for reacting to the selectionChanged() signal and the extracting the wanted index from the connected slot's arguments.

Cheers,
_