c++ - QT 上下文菜单未与光标对齐
问题描述
当我右键单击时,会出现一个上下文菜单,但它没有与光标对齐。我希望上下文菜单的角落出现在光标所在的位置。在这里你可以看到它离右边很远:
这是显示上下文菜单的方法:
void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
QMenu menu("contextMenu", this);
QAction deleteItem("Delete", this);
menu.addAction(&deleteItem);
connect(&deleteItem, SIGNAL(triggered()), this, SLOT(deleteItem()));
menu.exec(mapToGlobal(pos));
}
谢谢您的帮助!
解决方案
该pos
变量是相对于viewport()
的QListWidget
所以你必须使用的mapToGlobal()
方法QListWidget
:
menu.exec(ui->listWidget->viewport()->mapToGlobal(pos));
推荐阅读
- javascript - window.open with _blank 在 Firefox 中打开两个标签页
- javascript - 如果参数是另一个函数的返回值,该函数将不起作用
- linux - 使用 grep 精确匹配单词
- r - 将数据框列名更改为小写时使用 set_names 与 mutate(colnames)
- android - DataSnapShot 对象的值返回 0 而不是实际值
- azure - Azure 容器注册表 - 删除除 2 之外的所有映像
- python-3.x - 使用元组对实例的“装饰”列表进行排序 - Zelle Ex。11.9
- ubuntu - Unable to Run Amethyst Starter Template: 'VkError(ERROR_INCOMPATIBLE_DRIVER)'
- macos - SceneKit 在 macOS Catalyst 应用程序中是否可以继续工作?
- android - android design library 28.0.0 causes app to crash