一、QWidget产生右键菜单
默认我ContextMenuPolicy是DefaultContextMenu,这个需要重写contextMenuEvent(QContextMenuEvent *event)事件。
这里采用的是Qt::CustomContextMenu,就需要自己去响应customContextRequested(const QPoint&)这个信号
m_pActionRemove = new QAction(tr("DELETE"), this);
m_pMenu = new QMenu(this);
m_pMenu->addAction(m_pActionRemove);
this->setContextMenuPolicy(Qt::CustomContextMenu);
bool t1 = connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(popMenu(const QPoint&)));
void CLXTaskItemWidget::popMenu(const QPoint&)
{
if (!isDelete) {
QAction *a_c = m_pMenu->exec(QCursor::pos());
if (a_c != NULL) {
bool r2 = disconnect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(popMenu(const QPoint&)));
bool r3 = disconnect(m_pActionRemove, SIGNAL(triggered()), this, SLOT(actionRemoveSlot()));
QString ss = a_c->text();
}
}
return;
}
在actionRemoveSlot中执行删除自己的操作,并赋值isDelete为true,但即使是这样,最后一样会进入popMenu中从而引发崩溃。
二、解决办法:
只能是在这个QWidget的外面包一个父类,相对应的右键菜单都放到父类中去。这样子类自己被删了也不会出现崩溃的情况。