#include <QtWidgets>
class Path;
public:
Node(Path* path, int index);
protected:
private:
static const qreal rad;
Path* path;
int index;
};
const qreal Node::rad = 5;
public:
void updateElement(int index, const QPointF& pos);
private:
};
Node::Node(Path* path, int index)
path(path) , index(index)
{
setZValue(1);
setBrush(Qt::green);
}
{
if (change == ItemPositionChange) {
path->updateElement(index, value.toPointF());
}
}
{
for (int i = 0; i < path.elementCount(); ++i) {
auto node = new Node(this, i);
node->setPos(path.elementAt(i));
scene->addItem(node);
}
setPen
(QPen(Qt
::red,
1.75));
}
void Path
::updateElement(int index,
const QPointF &pos
) {
path.setElementPositionAt(index, pos.x(), pos.y());
setPath(path);
}
int main(int argc, char *argv[])
{
path.moveTo(0, 0);
path.cubicTo(-30, 70, 35, 115, 100, 100);
path.lineTo(200, 100);
path.cubicTo(200, 30, 150, -35, 60, -30);
scene->addItem(new Path(path, scene));
view
->setRenderHint
(QPainter::Antialiasing);
view->resize(600, 400);
view->show();
return app.exec();
}
#include <QtWidgets>
class Path;
class Node: public QGraphicsEllipseItem {
public:
Node(Path* path, int index);
protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
static const qreal rad;
Path* path;
int index;
};
const qreal Node::rad = 5;
class Path: public QGraphicsPathItem {
public:
Path(const QPainterPath& path, QGraphicsScene* scene);
void updateElement(int index, const QPointF& pos);
private:
QPainterPath path;
};
Node::Node(Path* path, int index)
: QGraphicsEllipseItem(-rad, -rad, 2*rad, 2*rad),
path(path) , index(index)
{
setZValue(1);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
setBrush(Qt::green);
}
QVariant Node::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange) {
path->updateElement(index, value.toPointF());
}
return QGraphicsEllipseItem::itemChange(change, value);
}
Path::Path(const QPainterPath& path, QGraphicsScene* scene)
: QGraphicsPathItem(path), path(path)
{
for (int i = 0; i < path.elementCount(); ++i) {
auto node = new Node(this, i);
node->setPos(path.elementAt(i));
scene->addItem(node);
}
setPen(QPen(Qt::red, 1.75));
}
void Path::updateElement(int index, const QPointF &pos)
{
path.setElementPositionAt(index, pos.x(), pos.y());
setPath(path);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPainterPath path;
path.moveTo(0, 0);
path.cubicTo(-30, 70, 35, 115, 100, 100);
path.lineTo(200, 100);
path.cubicTo(200, 30, 150, -35, 60, -30);
auto scene = new QGraphicsScene();
scene->addItem(new Path(path, scene));
auto view = new QGraphicsView(scene);
view->setRenderHint(QPainter::Antialiasing);
view->resize(600, 400);
view->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
?
Bookmarks