我尝试在qraphicsView中的qt中实现对点,圆,线,椭圆等所有项目的复制和粘贴操作,但收到错误:
error: void value not ignored as it ought to be storedItem = createCopy(item);
error: prototype for 'QGraphicsItem* CadGraphicsScene::createCopy(QGraphicsItem*)' does not match any in class 'CadGraphicsScene'"QGraphicsItem* CadGraphicsScene::createCopy(QGraphicsItem* item)"
我在CadGraphicsScene中的代码是:
void CadGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent* evt)
{
QList<QGraphicsItem*> itemsAt = items(evt->scenePos());
QGraphicsItem* item = itemsAt.isEmpty() ? 0x00 : itemsAt.first();
static QGraphicsItem* storedItem = 0x00;
QMenu menu;
if(item)
{
menu.addAction("Copy");
menu.addAction("Cut");
}
if(storedItem)
{
menu.addAction("Paste");
}
QAction* action = menu.exec(evt->screenPos());
if(action != 0x00)
{
if(action->text() == "Copy")
{
if(storedItem)
delete storedItem;
storedItem = createCopy(item);
}
else if(action->text() == "Cut")
{
if(storedItem)
delete storedItem;
storedItem = item;
removeItem(item);
}
else if(action->text() == "Paste")
{
QGraphicsItem* item = createCopy(storedItem);
if(item) {
addItem(item);
item->setPos(evt->scenePos());
}
}
}
}
QGraphicsItem* CadGraphicsScene::createCopy(QGraphicsItem* item)
{
if(item == 0x00)
return 0x00;
if(item->type() == Point::Type) //PointItem
{
Point* rectItem = (Point*)item;
Point* copy = new Point(item->parentItem());
copy->setRect(rectItem->rect());
copy->setPen(rectItem->pen());
copy->setBrush(rectItem->brush());
return copy;
}
if(item->type() == Line::Type) //LineItem
{
Line* rectItem = (Line*)item;
Line* copy = new Line(item->parentItem());
copy->setRect(rectItem->rect());
copy->setPen(rectItem->pen());
copy->setBrush(rectItem->brush());
return copy;
}
if(item->type() == Circle::Type) //CircleItem
{
Circle* rectItem = (Circle*)item;
Circle* copy = new Circle(item->parentItem());
copy->setRect(rectItem->rect());
copy->setPen(rectItem->pen());
copy->setBrush(rectItem->brush());
return copy;
}
if(item->type() == Ellipse::Type) //EllipseItem
{
Ellipse* rectItem = (Ellipse*)item;
Ellipse* copy = new Ellipse(item->parentItem());
copy->setRect(rectItem->rect());
copy->setPen(rectItem->pen());
copy->setBrush(rectItem->brush());
return copy;
}
if(item->type() == mText::Type) //TextItem
{
mText* rectItem = (mText*)item;
mText* copy = new mText(item->parentItem());
copy->setRect(rectItem->rect());
copy->setPen(rectItem->pen());
copy->setBrush(rectItem->brush());
return copy;
}
return 0x00;
}
CadGraphicsScene.h是:
#ifndef CADGRAPHICSSCENE_H
#define CADGRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QXmlStreamWriter>
#include <QUndoStack>
#include <QLabel>
#include <QFrame>
#include "cadcommandadd.h"
#include "cadcommanddelete.h"
#include "cadcommandmove.h"
class CadGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit CadGraphicsScene(QObject *parent, QUndoStack *);
enum Mode { NoMode, PointMode, LineMode, CircleMode, EllipseMode, TextMode };
QFont font() const
{
return myFont;
}
QColor textColor() const
{
return myTextColor;
}
void setTextColor(const QColor &color);
void setFont(const QFont &font);
void deleteItems();
void writeStream(QXmlStreamWriter *stream);
void readStream(QXmlStreamReader *stream);
public slots:
void setMode(Mode mode);
void selectItems();
void editorLostFocus(mText *item);
protected:
void contextMenuEvent(QGraphicsSceneContextMenuEvent* evt);
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
void setFlags();
void areItemsSelectable(bool);
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);
void createCopy(QGraphicsItem* item);
signals:
void textInserted(QGraphicsTextItem *item);
void itemSelected(QGraphicsItem *item);
private:
Mode entityMode;
QUndoStack *mUndoStack;
bool mFirstClick;
bool mSecondClick;
bool mThirdClick;
bool mPaintFlag;
QVector<QPointF> stuff;
QPointF start_p, mid_p, end_p, move_p, check_p;
QPen paintpen, linePen;
QList<QGraphicsItem *> itemList;
Point *pointItem;
Line *lineItem;
Circle *circleItem;
Ellipse *ellipseItem;
mText *textItem;
QColor myTextColor;
QFont myFont;
typedef QPair<QGraphicsItem *, QPointF> itemPos;
QList<itemPos> selectedItems;
private:
QAction *cutAct;
QAction *copyAct;
};
#endif // CADGRAPHICSSCENE_H
请帮我解决这个问题。
答案 0 :(得分:1)
你有不匹配的原型。您在标头中返回void
,但在源中返回QGraphicsItem*
。这也是编译器试图编写的内容。
void createCopy(QGraphicsItem* item);
QGraphicsItem* CadGraphicsScene::createCopy(QGraphicsItem* item)
解决方案是将标题中的签名更改为:
QGraphicsItem* createCopy(QGraphicsItem* item);