void asdfghjkl::contextMenuEvent(QContextMenuEvent *event)
{
//把鼠标位置转化成相对于QWidget的位置,然后判断是否在QListWidget内.
QPoint i = ui.listWidget->mapFromGlobal(event->globalPos());
QRect rect(QPoint(0, 0),ui.listWidget->size());
if (rect.contains(i))
{
QListWidgetItem* item = ui.listWidget->itemAt(i);获取右键点击位置
if (item != NULL) //点击位置不为空
{ //创建一个菜单,后面不在解释.}
a = ui.listWidget->currentRow();;
m_contextMenu = new QMenu;
m_addAction1 = new QAction(QIcon(":/new/prefix3/image/like_32px_1101682_easyicon.net.png"), QStringLiteral("添加到我喜欢"), this);// 添加项
connect(m_addAction1, SIGNAL(triggered()), this, SLOT(tianjia())); 关联该项的事件
m_contextMenu->addAction(m_addAction1);把该项放入菜单
m_contextMenu->exec(event->globalPos());
}
}