Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);

    QList<QGraphicsItem > list1= this->selectedItems();
    itemListSize = list1.size();
    out<<itemListSize;
    foreach( QGraphicsItem
    item, list1)
    {
    out << item->x();
    out << item->y();
    }
    [/code]

    and for pasting it back i use the following code

    [code]QFile file("fileName.dat");
    QList<QGraphicsItem *> list1 = this->selectedItems();
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);
    in>>itemListSize;
    foreach(QGraphicsItem *item, list1)
    {
    in >> item->x();
    in >> item->y();
    scene1->addItem(item);
    }

    file.close();[/code]

    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

    [code]{
    if (mouseEvent->button() != Qt::LeftButton)
    return;

     DiagramItem *item;
     switch (myMode) {
    

    case InsertItem:
    item = new DiagramItem(myItemType);
    item->setBrush(myItemColor);
    addItem(item);
    item->setPos(mouseEvent->scenePos());
    emit itemInserted(item);
    break;

       ;  
     }
    
     QGraphicsScene::mousePressEvent(mouseEvent);
    

    }
    [/code]

    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);
    }
    setPolygon(myPolygon);

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

    }[/code]

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


Log in to reply