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 8998879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:05:53+00:00 2026-06-16T00:05:53+00:00

i use Reading/writing QObjects is it true? i serialize a class with it but

  • 0

i use Reading/writing QObjects
is it true?
i serialize a class with it but when deserialize it isn’t the original class!

what can i do?

this is my base class header:

class Base : public QObject
{
    Q_OBJECT
public:
    explicit Base(QObject *parent = 0);


};
QDataStream &operator<<(QDataStream &ds, const Base &obj);
QDataStream &operator>>(QDataStream &ds, Base &obj) ;

and .cpp is:

Base::Base(QObject *parent) :
    QObject(parent)
{
}
QDataStream &operator<<(QDataStream &ds, const Base &obj) {
    for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
        if(obj.metaObject()->property(i).isStored(&obj)) {
            ds << obj.metaObject()->property(i).read(&obj);

        }
    }
    return ds;
}
QDataStream &operator>>(QDataStream &ds, Base &obj) {
    QVariant var;
    for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
        if(obj.metaObject()->property(i).isStored(&obj)) {
            ds >> var;
            obj.metaObject()->property(i).write(&obj, var);
        }
    }
    return ds;
}

and i have a student class that inherit from base :

class student : public Base
{
public:
    student();
    int id;
    QString Name;
};

and it is my main :

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    student G;
    student G2;
    G.id=30;
    G.Name="erfan";
    qDebug()<<G.id<<G.Name;
    QFile file("file.dat");
    file.open(QIODevice::WriteOnly);

    QDataStream out(&file);   // we will serialize the data into the file
    out <<G;
    qDebug()<<G2.id<<G2.Name;
    file.close();
    file.open(QIODevice::ReadOnly);
    out>>G2;
    qDebug()<<G2.id<<G2.Name;

    return a.exec();
}

and it is my output:

30 "erfan" 
1498018562 "" 
1498018562 ""
  • 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-06-16T00:05:54+00:00Added an answer on June 16, 2026 at 12:05 am

    You must make id and Name as Q_PROPERTY to handle it with metaObject property system:

    class student : public Base
    {
        Q_OBJECT // Q_OBJECT macro will take care of generating proper metaObject for your class
        Q_PROPERTY(int id READ getId WRITE setId)
        Q_PROPERTY(QString Name READ getName WRITE setName)
    public:
        student();
        int getId() const { return id; }
        void setId(int newId) { id = newId; }
        QString getName() const { return Name; }
        void setName(const QString &newName) { Name = newName; }
    
    private:
        int id;
        QString Name;
    };
    

    Now properties should be handled in proper way.

    See http://doc.qt.io/qt-5/properties.html for detailed information.

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

Sidebar

Related Questions

I use reading and writing to the database without any problems . But I
Possible Duplicate: Reading and writing images to an SQLite DB for iPhone use How
Suppose both parent and child use one pipe for writing and reading means when
When reading an NSString from a file I can use initWithContentsOfFile:usedEncoding:error: and it will
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid
I'm sure this can't be too hard; but I'm struggling. The error is Exception
While I realize Adobe discourages use of the application directory for reading/writing, I have
Possible Duplicate: Reading and writing images to an SQLite DB for iPhone use How
I am using this adobe example under reading and writing objects to try and
I'm studying UML and I've been reading about use cases. All of the examples

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.