我正在向 mContextMenu 添加一些操作,如下所示:
void RadarView::prepareMainMenu() {
mContextMenu = new QMenu();
//showLineAction->setShortcut(QKeySequence("Alt+Shift+L"));
mpStartRulerAction = new QAction(QObject::tr("Start Ruler"), this);
mContextMenu->addAction(mpStartRulerAction);
connect(mpStartRulerAction, SIGNAL(triggered()), this,
SLOT(menuStartRulerClicked()));
mpStartRulerAction->setProperty("TYPEVIEW", MV_StartRuler);
mpEndRulerAction = new QAction(QObject::tr("End Ruler"), this);
mContextMenu->addAction(mpEndRulerAction);
connect(mpEndRulerAction, SIGNAL(triggered()), this, SLOT(menuEndRulerClicked()));
mpEndRulerAction->setProperty("TYPEVIEW", MV_EndRuler);
mpCriticalRegionAction = new QAction(QObject::tr("Critical Region"), this);
mContextMenu->addAction(mpCriticalRegionAction);
connect(mpCriticalRegionAction, SIGNAL(triggered()), this, SLOT(menuDefineCriticalRegionClicked()));
mpCriticalRegionAction->setProperty("TYPEVIEW", MV_Critical_Region);
mpAScopeAction = new QAction(QObject::tr("A-Scope Line"), this);
mContextMenu->addAction(mpAScopeAction);
connect(mpAScopeAction, SIGNAL(triggered()), this, SLOT(menuAddAScopeLine()));
mpAScopeAction->setProperty("TYPEVIEW", MV_A_Scope);
}
图形视图的上下文菜单事件实现如下:
void RadarView::contextMenuEvent(QContextMenuEvent * event) {
LOGGER_START
//check if the item has its own context menu...
QList<QGraphicsItem*> items = this->items(event->pos());
if (items.size() != 0) {
bool isValid = true;
for (int i = 0; i < items.size(); ++i) {
QGraphicsRulerLineItem *rulerLineItem = NULL;
rulerLineItem = dynamic_cast<QGraphicsRulerLineItem*> (items[i]);
if (rulerLineItem != NULL || dynamic_cast<AScopeLineItem*> (items[i]) || dynamic_cast<PpiTargetItem*> (items[i])){
isValid = false;
}
else {
PpiPlotItem *targetItem = NULL;
targetItem = dynamic_cast<PpiPlotItem*> (items[i]);
if (targetItem != NULL) {
isValid = false;
if (currentVisibleMenuItems.contains(0)) {
startItem = targetItem;
} else if (currentVisibleMenuItems.contains(1)) {
endItem = targetItem;
}
}
}
}
if (isValid == true) {
if(currentVisibleMenuItems.size() ==0)
return;
//Get last clicked Position.
mLastClickedPos = event->pos();
//show menu of RadarView...
mpRightClickAction = mContextMenu->exec(event->globalPos());
} else {
QGraphicsView::contextMenuEvent(event);
}
}
LOGGER_END
}