QTableWidgetItem.itemAt(pos)始终在contextMenu请求中返回0

时间:2011-08-23 13:14:03

标签: c++ qt qtablewidget

我有一个插槽,可以创建给定点的上下文菜单。但是,无论我使用哪种方法从点返回位置,table-> itemAt(pos)总是返回0.有没有办法打印出表行/列的位置,这样我可以对一些偏移进行反向工程? / p>

我尝试了每一种排列,并且总是执行“没有项目”......

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
}
....
}

1 个答案:

答案 0 :(得分:2)

您对itemAt()的调用应使用 point ,它位于窗口小部件的本地坐标中,而不是 pos ,它位于全局坐标中。