-1
我在執行復制操作中graphicsView不同的實體,如點,橢圓形,圓形等。QT,但得到一個錯誤的foreach:從「的QGraphicsItem *」的無效轉換到「QGraphicsItemGroup *
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
幫助我走出來尋求這個:)
什麼'entityMode','mUndoStack','contentsContextMenuEvent'等與這個問題有關?查看[sscce.org](http://sscce.org/)和[「最小,完整,可驗證示例」](http://stackoverflow.com/help/mcve),以獲得集中幫助的最佳方式帶有編程問題的互聯網。 – HostileFork 2014-10-28 10:50:16
'itemList'被聲明爲'QList'。你寫'foreach(QGraphicsItemGroup * item,itemList)'。編譯器說它不能將'QGraphicsItem *'強制轉換爲'QGraphicsItemGroup *'。你需要什麼額外的信息? –
Ezee
2014-10-28 10:54:08