简体   繁体   English

QT 上下文菜单未与光标对齐

[英]QT context menu not aligned with cursor

When I right click, a context menu appears but it is not aligned with the cursor.当我右键单击时,会出现一个上下文菜单,但它没有与光标对齐。 I want the corner of the context menu to appear right where the cursor is.我希望上下文菜单的一角出现在光标所在的位置。 Here you can see it is quite far off to the right:在这里你可以看到它离右边很远:

在此处输入图片说明

Here is the method where the context menu is shown:这是显示上下文菜单的方法:

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)); 

}

Thanks for the help!谢谢您的帮助!

pos变量是相对于viewport()中的QListWidget ,所以你必须使用mapToGlobal()的方法QListWidget

menu.exec(ui->listWidget->viewport()->mapToGlobal(pos));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM