jim
jim

Reputation: 1

QTableWidgetItem.itemAt(pos) always returns 0 on contextMenu request

I have a slot which creates a context menu given a point. However, no matter which method I use to return the position from the point, table->itemAt(pos) always returns 0. Is there any way to print out the tables row/columns positions so I can reverse engineer some offsets?

I tried every permutation possible and 'no item' is always executed....

void test::newContextMenu(const QPoint& point)
{
QPoint pos = table_->viewport()->mapFromGlobal(point); //also tried
//table_->mapFromGlobal(point), table_>viewport()->mapToGlobal(point)
//table_->mapToGlobal(point), and QCursor::pos()

QAction* action tableMenu_->exec(pos);
if(action == XX)
{
QTableWidgetItem* item = table_->itemAt(pos);
if(!item)
{
 ///no item
}
....
}

Upvotes: 0

Views: 557

Answers (2)

ISM
ISM

Reputation: 11

Try to use:

QPoint tablePoint = table->viewport()->mapFromParent(event->pos()); QModelIndex indexAt = table->indexAt(tablePoint);
QTableWidgetItem *itemAt =table->itemAt(tablePoint);

Upvotes: 1

Chris
Chris

Reputation: 17535

Your call to itemAt() should be using point, which is in coordinates local to your widget, not pos which is in global coordinates.

Upvotes: 2

Related Questions