Unsolved How can I Copy/Paste QgraphicsItems
-
How do I Copy the QGraphicsitems and paste it on the same Scene.In my program there have QGraphicsRectItem,QGraphicsLineItem,qgraphicstextitem.Now I need to select any Qgraphicitem and click copy button to copy and click paste button for paste it on the same scene.How can I do this.here is my sample code.
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QGraphicsScene> #include <QGraphicsRectItem> #include <QGraphicsLineItem> #include <QWidget> #include <QtCore> #include <QtGui> #include <QSvgGenerator> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); void create_rect(QRectF qrectf); private slots: void on_CopyButton_clicked(); void on_PasteButton_clicked(); private: Ui::Widget *ui; QGraphicsScene *scene; QGraphicsEllipseItem *elipse; QGraphicsRectItem *rect; QString path; QGraphicsLineItem line; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" #include "readsvg.h" #include <QCursor> #include <QFileDialog> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); QBrush redBrush(Qt::red); QBrush blueBrush(Qt::blue); QPen blackPen(Qt::black); blackPen.setWidth(6); elipse = scene->addEllipse(20,20,100,100,blackPen,redBrush); elipse->setFlag(QGraphicsItem::ItemIsMovable, true); rect = scene->addRect(-20,-10,200,100,blackPen,blueBrush); rect->setFlag(QGraphicsItem::ItemIsMovable, true); rect->setFlag(QGraphicsItem::ItemIsSelectable,true); //QGraphicsLineItem lines = scene.addLine(new QLineF(-10, -10, 20, 20)); // line = scene->addLine(-10, -10, 20, 20); } Widget::~Widget() { delete ui; } void Widget::on_CopyButton_clicked() { } void Widget::on_PasteButton_clicked() { }
-
Hi,
You should copy the properties of the element you are copying and then create a new element on past and set these properties on it.
-
@SGaist said in How can I Copy/Paste QgraphicsItems:
properties of the element you are copying and then create a new element on past and set these
Can you please give me some sample code to solve this problem.
-
@CorD-SaC
Hi
QGraphicsItem is not copyable.You just copy what you need to make it a clone
Say for your
elipse = scene->addEllipse(20,20,100,100,blackPen,redBrush);You you take the pen and brush ( it has functions to read them)
and its size.
and give to you new object when you construct it. -
@mrjj said in How can I Copy/Paste QgraphicsItems:
you new object when you construct
Can you give me some sample code to implement that. because I'm new to QT.so please help me sir
-
If you are cloning a QGraphicsRectItem then get the rect from it and apply it to your cloned item using setRect. Same goes for the pen and brush.
-
@CorD-SaC
Its not very complicated and very much like code you have now
(pseudo code - didnt not compile it :- there will be errors)void Clone() { //Get the selected object QList<QGraphicsItem*> selitems =scene->selectedItems(); if (selitems.size() == 0 ) { /*ERROR(none selected)*/ return; } //(for easy talk, we assume you ONLY select one : ) QGraphicsItem* item = selitems.first(); //We need to find out what it is. ( since it returns a base class but they are actually differnt) QGraphicsEllipseItem* elitem = graphicsitem_cast<QGraphicsEllipseItem*>(item) if (elitem) { // if its not NULL it means it was a QGraphicsEllipseItem. QRectF thesize = item->rect(); QPen pen = item->pen(); QBrush brush = item->brush(); // anything else u like scene->addEllipse(thesize, pen, brush); // make the clone return; // leave } QGraphicsRectItem* rectitem = graphicsitem_cast<QGraphicsRectItem*>(item) if (rectitem) { // if its not NULL it means it was a QGraphicsRectItem. // copy what you want // add new to scene return; } // handle other types }
-
@mrjj said in How can I Copy/Paste QgraphicsItems:
clone
Thank you very much.I'll try to implement it
-
@CorD-SaC
super.
you might be able to compact it a little if same things to copy
but you need
graphicsitem_cast anyway to know what to insert
scene->addEllipse or scene->addRect anyway so
i like to just do pr type. -