Nominate our 2022 Qt Champions!

Encoding and decoding objects

  • Hi..I am looking for some direction how to do save my application and its objects. Quite new to Qt and I have not done this with C++ either (I have used apple and they use something called NSCoder to do this). I am having an application with a few forms to control standard CAD objects that I create (cone, cylinder etc). I want to 1)save my application and its obects 2) Be able to open them again. Its not Qsettings I need, something that will help me encode and decode Objects (not strings, texts etc) which is what most examples I find. Any direction would be appreciated. Thanks in advance.

  • Thank you. I will take a look.

  • Hi Lelev
    The links were very useful and I managed to write a class object to a file using QDataStream. But I am still lost with doing a few things and wonder if this is very advanced topic to find help online. So, I have a qt application that makes simple CAD objects, like cones and cylinders. Each of them have a specific data in a class and I managed to write them to a file (thanks to the links you provided). When I am trying to open this data, how do I refresh the main window and the forms used with this data? Do I need to delete these objects and create new mainwindow and new forms with this data? (or) do I write these forms as well in files (if so, its not clear how to use QDataStream). Sorry if these are basic questions. But any guidance would be helpful.

  • Lifetime Qt Champion

    @rbharadw What are these CAD objects? UI widgets?
    There is absolutely no need to recreate the main window.
    You need to create operator>>(...) operators for your CAD objects and then (pseudo-code):

    CAD cad(this);
    stream>>cad;, // I assume CAD is a widget and you want to show it

  • Hello Jsulm
    I apologies for the very poor description. You are correct, I meant, objects like cones, cylinders etc (Computer Aided Design) that I store them in a class called data (like conegeometry). I use widget forms to show this data, like diameter etc (called conegeometryforms). So what I have figured out so far is to stream the data using QDataStream into a binary file. I can also read from them. But what is not clear to me is how do I update the forms and the openGLwidget. How do people do this? Do they delete all the old forms and then make new ones with the new data? Do I need to make a document class to do this? I am not sure if there is any easier way.

  • Lifetime Qt Champion

    @rbharadw said in Encoding and decoding objects:

    Do they delete all the old forms and then make new ones with the new data?

    Since I don't know the exact use case I can't really comment on this. You can remove existing objects and add the new ones you read from binary files, or you can update the existing ones, it's up to you. Don't know what exact problem you have with this.

  • Thank you for all the replies. This was very helpful. No technical issue anymore, just a best practice query.

Log in to reply