Nominate our 2022 Qt Champions!

Copy and paste QGraphicItem on my QGraphicScene

  • hi i am a new to Qt, i couldn’t find how u will serialize selected items, if you copy an selected item like which i done below i just get a shallow copy of an item, how do we actually get this selected item and serialize it. plz show it with a small example

    QList<QGraphicsItem *> copiedItem = this->selectedItems();

    All i want to finally do is, i wanted to copy and paste QGraphicItem on my QGraphicScene .
    thanks in advance

  • Hi i am new to Qt, and i am working on cut, copy, paste plz give directions how to solve this

    This is how i copy an item and serialize it

    [code]QFile file("fileName.dat");;
    QDataStream out(&file);

    QList<QGraphicsItem > list1= this->selectedItems();
    itemListSize = list1.size();
    foreach( QGraphicsItem
    item, list1)
    out << item->x();
    out << item->y();

    and for pasting it back i use the following code

    [code]QFile file("fileName.dat");
    QList<QGraphicsItem *> list1 = this->selectedItems();;
    QDataStream in(&file);
    foreach(QGraphicsItem *item, list1)
    in >> item->x();
    in >> item->y();


    but i cannot get the items x and y position while reading data. Am i serializing in the correct way, plz help me out.
    And this is how i call the rect item using a mousePressEvent from my DiagramItem class

    if (mouseEvent->button() != Qt::LeftButton)

     DiagramItem *item;
     switch (myMode) {

    case InsertItem:
    item = new DiagramItem(myItemType);
    emit itemInserted(item);



    This is the code written for creating a polygon item in my DiagramItem's class constructor

    [code]myDiagramType = diagramType;
    QPainterPath path;
    switch (myDiagramType) {
    case Step:
    myPolygon << QPointF(-100, -100) << QPointF(100, -100)
    << QPointF(100, 100) << QPointF(-100, 100)
    << QPointF(-100, -100);

     setFlag(QGraphicsItem::ItemIsMovable, true);
     setFlag(QGraphicsItem::ItemIsSelectable, true);
     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);


    if suggested any changes that has to be done in the code will be much helpfull ;(

Log in to reply