Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Copy and paste QGraphicItem on my QGraphicScene

Copy and paste QGraphicItem on my QGraphicScene

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinzzz
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vinzzz
      wrote on last edited by
      #2

      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 ;(

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved