Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 856795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:17:37+00:00 2026-05-15T08:17:37+00:00

I am using the Qt QGraphicsScene class, adding pre-defined items such as QGraphicsRectItem ,

  • 0

I am using the Qt QGraphicsScene class, adding pre-defined items such as QGraphicsRectItem, QGraphicsLineItem, etc. and I want to serialize the scene contents to disk. However, the base QGraphicsItem class (that the other items I use derive from) doesn’t support serialization so I need to roll my own code. The problem is that all access to these objects is via a base QGraphicsItem pointer, so the serialization code I have is horrible:

QGraphicsScene* scene = new QGraphicsScene;
scene->addRect(QRectF(0, 0, 100, 100));
scene->addLine(QLineF(0, 0, 100, 100));
...
QList<QGraphicsItem*> list = scene->items();
foreach (QGraphicsItem* item, items)
{
  if (item->type() == QGraphicsRectItem::Type)
  {
    QGraphicsRectItem* rect = qgraphicsitem_cast<QGraphicsRectItem*>(item);
    // Access QGraphicsRectItem members here
  }
  else if (item->type() == QGraphicsLineItem::Type)
  {
    QGraphicsLineItem* line = qgraphicsitem_cast<QGraphicsLineItem*>(item);
    // Access QGraphicsLineItem members here
  }
  ...
}

This is not good code IMHO. So, instead I could create an ABC class like this:

class Item
{
public:
  virtual void serialize(QDataStream& strm, int version) = 0;
};

class Rect : public QGraphicsRectItem, public Item
{
public:
  void serialize(QDataStream& strm, int version)
  {
    // Serialize this object
  }
  ...
};

I can then add Rect objects using QGraphicsScene::addItem(new Rect(,,,));

But this doesn’t really help me as the following will crash:

QList<QGraphicsItem*> list = scene->items();
foreach (QGraphicsItem* item, items)
{
  Item* myitem = reinterpret_class<Item*>(item);
  myitem->serialize(...) // FAIL
}

Any way I can make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:17:38+00:00Added an answer on May 15, 2026 at 8:17 am

    I agree with the other posters, the QGraphicsItem could really be viewed as a view item and so separating your model data into its own class would probably be better.

    That said, I think your crash is caused by an inappropriate cast.

    If you do the following:

    Rect *r = new Rect();
    QGraphicsItem *qi = reinterpret_cast<QGraphicsItem*>(r);
    QGraphicsRectItem *qr = reinterpret_cast<QGraphicsRectItem*>(r);
    Item *i = reinterpret_cast<Item*>(r);
    qDebug("r = %p, qi = %p, qr = %p, i = %p", r, qi, qr, i);
    

    You should see that r == qi, r == qr, but r != i. If you think about how an object that multiply inherits is represented in memory, the first base class is first in memory, the second base class is second, and so forth. So the pointer to the second base class will be offset by [approximately] the sizeof the first base class.

    So to fix your code, I think you need to do something like:

    QList<QGraphicsItem*> list = scene->items();
    foreach (QGraphicsItem* item, items)
    {
      Rect* myrect = reinterpret_class<Rect*>(item);  // needed to figure out the offset to the Item part
      Item* myitem = reinterpret_class<Item*>(myrect);
      myitem->serialize(...);
    }
    

    This is one of many reasons, I like to avoid multiple inheritance whenever possible. I strongly recommend separating you model data, as previously recommended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 476k
  • Answers 476k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have found that: foreach (object obj in (IEnumerable)Marshal.BindToMoniker("ADs:")) {… May 16, 2026 at 4:50 am
  • Editorial Team
    Editorial Team added an answer Use a Set instead of a List if duplicates shouldn't… May 16, 2026 at 4:50 am
  • Editorial Team
    Editorial Team added an answer Take a look at this post from Sacha Barber's website:… May 16, 2026 at 4:50 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.