PDA

View Full Version : QTableView, selected row?



postb99
20th February 2014, 11:39
Hello,

I try to detect cases where there is a selection or none, on a QTableView which allows single rows selection.

What's wrong? Code below doesn't fail, but I don't get the popups "xLogWarning()" calls should produce...

I'm also wondering about the model giving me its value for selected row and column of my choice...

I also read this topic: http://www.qtcentre.org/threads/1046-QTableView-get-selected-row

Thanks for helping.



m_tableView = new QTableView();
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
m_tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);




// Get selected image
QItemSelectionModel* selectedModel = m_tableView->selectionModel();
if(!selectedModel->hasSelection()) {
xLogWarning(tr("No image selected"));
return;
}
xLogWarning(tr("There is an image selected"));
QModelIndexList selection = selectedModel->selectedIndexes();
xLogWarning(tr("selected row: %1").arg(selection[0].row()));
xLogWarning(tr("Selected image: %1").arg(m_model.getObjectIdAt(selection[0].row())));

postb99
25th February 2014, 15:16
Hello,

Doing debug right now, what's wrong? My "selectedIndex" variable below looks strange, and the same whatever I select a row or not before clicking on the button besides the QTableView.



QItemSelectionModel* selectedModel = m_tableView->selectionModel();
QModelIndex selectedIndex = selectedModel->currentIndex();


10080

Added after 19 minutes:

OK, my debug was wrong, the above code works well.