我正在使用graphicsView为qt中的点,椭圆,圆等不同实体实现复制操作但是出错了
error: invalid conversion from 'QGraphicsItem*' to 'QGraphicsItemGroup*
我在cadgraphicsview.cpp中复制操作的代码是:
void CadGraphicsScene::copy()
{
selectedItems.clear();
foreach(QGraphicsItemGroup *item, itemList){
QString str;
if (item->isSelected())
{
if(item->type() == Point::Type)
{
Point *pointItem = (Point *) item;
str = QString("Point %1 %2 ")
.arg(pointItem->scenePos().x())
.arg(pointItem->scenePos().y());
}
}
}
}
在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);
void cut();
void copy();
void paste();
void del();
protected:
void contentsContextMenuEvent(QContextMenuEvent *event);
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);
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;
QAction *pasteAct;
};
#endif // CADGRAPHICSSCENE_H
帮助我寻求这个:)
答案 0 :(得分:1)
您将itemList
定义为:
QList<QGraphicsItem *> itemList;
然后您尝试使用以下内容进行迭代:
foreach(QGraphicsItemGroup *item, itemList){
// ...
}
从文档中可以看到QGraphicsItemGroup inherits from QGraphicsItem。所以每个QGraphicsItemGroup&#34;都是&#34;的QGraphicsItem。但并非每个QGraphicsItem都是&#34; QGraphicsItemGroup。
如果要迭代该itemList,则item必须是QGraphicsItem,或者itemList必须是QGraphicsItemGroup。似乎既然你正在测试特定的类,那么前者可能就是你想要的。