在Qt中连接SLOTS

时间:2014-12-01 09:03:54

标签: c++ qt

我有一个上下文菜单和编辑菜单。我想连接上下文菜单插槽和编辑菜单。 像EDIT菜单一样有菜单项:剪切,复制和粘贴

我的上下文菜单插槽是:

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

//上下文菜单操作

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

1 个答案:

答案 0 :(得分:1)

如果您想在应用程序的“编辑”菜单中重复使用相同的广告位,请在构建时使用现有的cutActioncopyActionpasteAction操作。因此,如果您已为该操作建立了连接,则当用户从上下文菜单和“编辑”菜单触发操作时,将同时调用相同的插槽。