C++ Qt中的连接槽
我有一个上下文菜单和编辑菜单。我想将关联菜单插槽与编辑菜单连接。 与“编辑”菜单类似,“编辑”菜单也有菜单项:剪切、复制和粘贴 我的上下文菜单插槽是:C++ Qt中的连接槽,c++,qt,C++,Qt,我有一个上下文菜单和编辑菜单。我想将关联菜单插槽与编辑菜单连接。 与“编辑”菜单类似,“编辑”菜单也有菜单项:剪切、复制和粘贴 我的上下文菜单插槽是: void CadGraphicsScene::cut(getEntity *obj) { // id of item pasted is kept same as that of the item being cut removeItem(obj); clipboardStack::instance()->push(
void CadGraphicsScene::cut(getEntity *obj)
{
// id of item pasted is kept same as that of the item being cut
removeItem(obj);
clipboardStack::instance()->push(obj->clone(contextItemId));
}
void CadGraphicsScene::copy(getEntity *obj)
{
// id of item pasted is one more than total number of items in the scene
clipboardStack::instance()->push(obj->clone(++id));
}
void CadGraphicsScene::paste(const QPointF &pos)
{
// gets the items cut/copy from clipboardStack to paste
getEntity *pasteEntity = clipboardStack::instance()->pop();
if (pasteEntity->type() == Point::Type)
{
Point *itemPtr = dynamic_cast<Point *>(pasteEntity);
itemPtr->position = pos;
drawEntity(itemPtr);
}
if (pasteEntity->type() == Line::Type)
{
Line *itemPtr = dynamic_cast<Line *>(pasteEntity);
itemPtr->startP = pos;
/* calculates difference between startP of line being cut/copy and line
* being pasted for proper pasting of line
*/
differenceX = itemPtr->startP.x() - lineStartPoint.x();
differenceY = itemPtr->startP.y() - lineStartPoint.y();
itemPtr->endP = QPointF(lineEndPoint.x() + differenceX,
lineEndPoint.y() + differenceY);
drawEntity(itemPtr);
}
if (pasteEntity->type() == Circle::Type)
{
Circle *itemPtr = dynamic_cast<Circle *>(pasteEntity);
itemPtr->centerP = pos;
drawEntity(itemPtr);
}
if (pasteEntity->type() == Ellipse::Type)
{
Ellipse *itemPtr = dynamic_cast<Ellipse *>(pasteEntity);
itemPtr->p1 = pos;
drawEntity(itemPtr);
}
if (pasteEntity->type() == Text::Type)
{
Text *itemPtr = dynamic_cast<Text *>(pasteEntity);
itemPtr->position = pos;
drawEntity(itemPtr);
}
setMode(NoMode);
}
如果要重用应用程序“编辑”菜单中的相同插槽,只需在构建时使用现有的
cutAction
、copyAction
和pasteAction
操作即可。因此,如果您已经为该操作建立了连接,那么当用户从上下文菜单和编辑菜单触发操作时,将调用相同的插槽。我不确定我是否理解您的问题,但是如果您有c++11和qt 5+,您可以将信号连接到任何函数:connect(my_action,&QAction::triggered,[=]{/*调用任何函数或执行任何操作*/});
我已编辑了问题。请检查并重试,这不起作用。
void CadGraphicsScene::menuAction(QAction *action)
{
if (action == cutAction)
{
cut(static_cast<getEntity *>(contextItem));
}
else if (action == copyAction)
{
copy(static_cast<getEntity *>(contextItem));
}
else if (action == pasteAction)
{
paste(contextPosition);
}
}
void CadgraphicsScene::cut()
{
cutAction
}
connect(actionCut, SIGNAL(triggered), this, SLOT(cut()));