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

发布于 2024-12-01 08:49:35 字数 576 浏览 3 评论 0原文

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

我尝试了所有可能的排列,但总是执行“无项目”......

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘亓 2024-12-08 08:49:35

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

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

有木有妳兜一样 2024-12-08 08:49:35

尝试使用:

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

Try to use:

QPoint tablePoint = table->viewport()->mapFromParent(event->pos()); QModelIndex indexAt = table->indexAt(tablePoint);
QTableWidgetItem *itemAt =table->itemAt(tablePoint);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文