Search
 
SCRIPT & CODE EXAMPLE
 

C

qgraphicsscene save all items to file

QVariant item_to_variant(QGraphicsItem* item) {
  QVariantHash data;
  //save all needed attributes
  data["pos"] = item->pos();
  data["rotation"] = item->rotation();
  if(QGraphicsPixmapItem* pixmap_item = dynamic_cast<QGraphicsPixmapItem*>(item)) {
    data["type"] = "pixmap";
    data["pixmap"] = pixmap_item->pixmap();
  } else { /*...*/ }
 //...
  return data;
}

QGraphicsItem* item_from_variant(QVariant v) {
  QVariantHash data = v.toHash();
  QGraphicsItem* result;
  if (data["type"].toString() == "pixmap") {
    result = new QGraphicsPixmapItem();
    result->setPixmap(data["pixmap"].value<QPixmap>());
  } else { /*...*/ }
  result->setPos(data["pos"].toPointf());
  result->setRotation(data["rotation"].toDouble());
  //...
  return result;
}

void save_state() {
  QVariantList data_list;
  foreach(QGraphicsItem* item, items_list) {
    data_list << item_to_variant(item);
  }
  QSettings settings;
  settings.setValue("items", data_list);
}

void restore_state() {
  QSettings settings;
  foreach(QVariant data, settings.value("items").toList()) {
    QGraphicsItem* item = item_from_variant(data);
    scene->addItem(item);
    items_list << item;
  }

}
Comment

PREVIOUS NEXT
Code Example
C :: passing an array of unspecified number of variables to a function 
C :: snprintf with malloc 
C :: Returns number of values 
C :: how to get a string input in c 
C :: scanf autotrash c 
C :: float 
C :: c pass two dimensional array to function 
C :: array of string in c 
C :: difference between int main() and int main(void) 
C :: time random c 
C :: Recommended compiler and linker flags for GCC 
Dart :: how to diable flutter for web 
Dart :: TextStyle underline flutter 
Dart :: flutetr stepper color 
Dart :: flutter padding top and bottom 
Dart :: how to get the name of the day in flutter 
Dart :: Container border left 
Dart :: add bg image to scaffold flutter 
Dart :: How can I add shadow to the widget in flutter? 
Dart :: dart find element in list 
Dart :: flutter lock orientation for page 
Dart :: text field placeholder color flutter theme 
Dart :: flutter image asset 
Dart :: flutter column text direction 
Dart :: flutter text 
Dart :: dart almashtirish 
Dart :: how do you change the back button flutter 
Dart :: how to convert int/int to float dart 
Dart :: flutter baseline 
Dart :: flutter how to create copy button 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =