QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)
Hi,
I have an ui->tableView (set up with designer) and subclassed a QStandardItemModel to attach it, along with aQSortFilterProxyModel.
I want to make the items right-cklickable for a context menu. It nearly works - apart from a very annoying offset in the heigth that amounts to roughly half of a row's height. When I right-click an item in it's bottom half, the item below is given back.
In the dialog's constructor:
Code:
proxyModel->setSourceModel(fotoImport); //foto Import is the model
proxyModel->setDynamicSortFilter(true);
ui->tableView->setModel(proxyModel);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();
ui->tableView->verticalHeader()->hide();
ui->tableView->setSortingEnabled(true);
In the dialog's contextMenuEvent (QContextMenuEvent *e) I try this:
Code:
pos=ui->tableView->mapFrom(this,e->pos());
int row=ui->tableView->rowAt(pos.y());
int column=ui->tableView->columnAt(pos.x());
if (row==-1)return;
if (column==-1)return;
proxyIndex=this->proxyModel->mapToSource(index);
qDebug()<<"map from this: "<<fotoImport->itemFromIndex(proxyIndex)->text();
It furthermore might be important that my model contains small pictures.
I've set them up this way in the model:
Code:
item->setData(myPixmap.scaledToHeight(50,Qt::SmoothTransformation),Qt::DecorationRole);
Instead of
pos=ui->tableView->mapFrom(this,e->pos());
I've also tried
pos=ui->tableView->mapFromParent(e->pos());
which gives equal results and
pos=ui->tableView->mapFrom(ui->tableView, e->pos());
as well as all other mapFromXXX and mapToXXX possibilities.
The columnAt() works fine.
Any idea what I am doing wrong?
Re: QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)
The position is given relative to the view's viewport(), not to the view itself. Thus the "annoying offset".
SOLVED: Re: QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)
Smack dab in the middle, wysota - as usual. Thank you!
Code:
pos=ui->tableView->viewport()->mapFromGlobal(e->globalPos());
did the trick.