我正在为不同的点,圆,椭圆,线和文本实现剪切复制和粘贴操作。我已经实现了一点,但是当我对其他进程采用相同的程序时,它并没有起作用。不知道为什么。请帮我解决这个困惑。我的代码如下:
cadgraphicsscene.cpp
void CadGraphicsScene::cut()
{
foreach (QGraphicsItem *item, itemList)
{
if (item->isSelected())
{
// Cuts the selected item
removeItem(item);
update();
}
}
}
void CadGraphicsScene::copy()
{
foreach (QGraphicsItem *item, itemList)
{
QString str;
// Stores the Point
if (item->isSelected() == Point::Type)
{
str = QString("Point copy p(%1,%2)")
.arg(item->x())
.arg(item->y());
}
if (item->isSelected() == Circle::Type)
{
Circle *circleItem = dynamic_cast<Circle *>(item);
str = QString("Circle copy c(%1,%2) r(%3)")
.arg(circleItem->center_p.x())
.arg(circleItem->center_p.y())
.arg(circleItem->radius);
}
}
}
void CadGraphicsScene::paste()
{
foreach (QGraphicsItem *item, itemList)
{
// Calls setNewItem function
setNewItem(item);
}
}
void CadGraphicsScene::setNewItem(QGraphicsItem *item)
{
static int id = 0;
if(item->isSelected() == PointMode)
{
// Create a New point
pointItem = new Point(++id);
pointItem->setPos(item->scenePos());
itemList.append(pointItem);
mUndoStack->push(new CadCommandAdd(this, pointItem));
}
if(item->isSelected() == CircleMode)
{
// Create a New point
circleItem = new Circle(id, start_p, _rad);
itemList.append(circleItem);
mUndoStack->push(new CadCommandAdd(this, circleItem));
}
}
point.cpp
#include "point.h"
Point::Point(int i)
{
// assigns id
id = i;
}
int Point::type() const
{
// Enable the use of qgraphicsitem_cast with point item.
return Type;
}
QRectF Point::boundingRect() const
{
// bounding rectangle for point
qreal penwidth = 1;
return QRectF(-1 - penwidth /2, -1 - penwidth/2,
2 + penwidth, 2 + penwidth);
}
void Point::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
// draws/paints the point
QPen paintpen;
if (isSelected())
paintpen.setColor(Qt::red);
else
paintpen.setColor(Qt::black);
paintpen.setWidth(2);
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::SolidPattern);
painter->setPen(paintpen);
painter->drawEllipse(boundingRect());
}
circle.cpp
#include "circle.h"
Circle::Circle(int i, QPointF p1, QPointF p2)
{
// assigns id
id = i;
/* set values of center point, end point
and calculate radius of circle */
center_p = p1;
end_p = p2;
radius = qSqrt(qPow((end_p.x()-center_p.x()), 2)
+ qPow((end_p.y()-center_p.y()), 2));
}
Circle::Circle(int i, QPointF p1, qreal rad)
{
// assigns id
id = i;
/* set values of center point
and radius of circle */
center_p = p1;
radius = rad;
}
int Circle::type() const
{
// Enable the use of qgraphicsitem_cast with circle item.
return Type;
}
QRectF Circle::boundingRect() const
{
// bounding rectangle for circle
return QRectF((center_p.x()-radius), (center_p.y()-radius),
(2*radius), (2*radius));
}
void Circle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
// draws/paints the path of circle
QPen paintpen(Qt::black);
paintpen.setWidth(1);
painter->setRenderHint(QPainter::Antialiasing);
if (isSelected())
{
// sets brush for center point
painter->setBrush(Qt::SolidPattern);
paintpen.setColor(Qt::red);
painter->setPen(paintpen);
painter->drawEllipse(center_p, 2, 2);
// sets pen for circumference
paintpen.setStyle(Qt::DashLine);
paintpen.setColor(Qt::black);
painter->setBrush(Qt::NoBrush);
painter->setPen(paintpen);
painter->drawEllipse(center_p, radius, radius);
}
else
{
painter->setBrush(Qt::SolidPattern);
paintpen.setColor(Qt::black);
painter->setPen(paintpen);
painter->drawEllipse(center_p, 2, 2);
painter->setBrush(Qt::NoBrush);
painter->drawEllipse(center_p, radius, radius);
}
}
答案 0 :(得分:0)
函数QGraphicsItem::itemIsSelected表示当前是否选择了该项并返回bool值。
因此,调用以下行对于检查项目类型无效: -
if (item->isSelected() == Point::Type)
if (item->isSelected() == Circle::Type)
如果要检查项目类型,请使用函数QGraphicsItem::type()