PDA

View Full Version : QTableView handling both selectionchange and itempressed event



Cupidvogel
13th June 2015, 20:20
Hi, I have a QTableView with rows of data stored. When the user clicks on a row, it shows an alert regarding the data stored in that row. I want the same alert to be shown when the user navigates to that row by pressing up/down arrow keys as well, so I bound the same slot to the currentRowChanged signal. This is my code:



connect(fTableView, SIGNAL(itemPressed(QModelIndex)), this, SLOT(showAlertFromPress(QModelIndex)));
connect(fTableView->selectionModel(),SIGNAL(currentRowChanged(QModelIn dex,QModelIndex)),
this,SLOT(showAlertFromSelection(QModelIndex,QMode lIndex)))

void QueryView::itemPressed(QModelIndex index)
{
int i = index.row();
fData = fVector[i];
//alert(data);
}

void QueryView::showAlertFromSelection(QModelIndex oldIndex, QModelIndex newindex)
{
int i = fTableView->currentIndex().row();
fData = fVector[i];
//alert(data);
}


This works fine, but as expected, for clicks on items different from the ones currently chosen, it fires the same event twice, one for selection change, and one for the click, as a result two alerts are being shown. How do I prevent this? If I bind only the selection change handler, it works almost fine, except that if I click an item, the alert shows up alright, but subsequent clicks on that item don't show the alert again because the selection is not changing.

I tried using a separate variable that is changed whenever the selection changes to reflect the latest selection. Then in the itemPressed handler, I check whether the index is same as that variable, if so, it should signify, that I am clicking on the already selected item. But problem is, the selection handler fires before the itemPressed event even on clicking. :(