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. Save object to a file

Save object to a file

Scheduled Pinned Locked Moved General and Desktop
37 Posts 2 Posters 13.1k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    If you want to use streams with your custom objects, you have to define the QTextStream's operator for them otherwise QTextStream won't be able to serialize them.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dmon
      wrote on last edited by
      #3

      thanks I added this function to define toString

      QString Film::toString()
      {
      return m_title + " " + m_duration + " " + m_director + " " + m_releaseDate.toString();
      }

      now I am getting error:
      debug/filminput.o:C:\Unisa\COS3711\assignments\Ass1Q1-build-desktop/../Ass1Q1/filminput.cpp:63: undefined reference to `FilmInput::saveFilm(Film&)'
      collect2: ld returned 1 exit status
      mingw32-make[1]: *** [debug\Ass1Q1.exe] Error 1
      mingw32-make: *** [debug] Error 2
      The process "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
      Error while building project Ass1Q1 (target: Desktop)
      When executing build step 'Make'

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        School work ?

        It seems the compiler doesn't know about saveFilm

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dmon
          wrote on last edited by
          #5

          Yes it's an assignment I'm doing. I don't understand how it doesn't see it because saveFilm is a function in class FilmWriter which is included in class definition for class FilmInput which is has a function which calls saveFilm.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Without the code it's difficult to comment

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dmon
              wrote on last edited by
              #7

              @#ifndef FILM_H
              #define FILM_H

              #include <QWidget>
              #include <QString>
              #include <QDate>

              class Film:public QWidget{
              public:
              Film(QString t,int dur,QString dir,QDate r);
              Film();
              void setTitle(QString t);
              void setDuration(int dur);
              void setDirector(QString dir);
              void setReleaseDate(QDate r);
              QString getTitle() const;
              int getDuration() const;
              QString getDirector() const;
              QDate getReleaseDate() const;
              QString toString();

              private:
              QString m_title;
              int m_duration;
              QString m_director;
              QDate m_releaseDate;

              };

              #endif // FILM_H

              #ifndef FILMWRITER_H
              #define FILMWRITER_H
              #include "Film.h"
              #include <QtGui>
              #include <QFile>

              class FilmWriter: public Film{

              public:
              void saveFilm(Film& f);

              };
              #endif // FILMWRITER_H

              #ifndef FILMINPUT_H
              #define FILMINPUT_H

              #include <QMainWindow>
              #include "Film.h"
              #include "FilmWriter.h"
              #include <QLabel>
              #include <QTextEdit>
              #include <QPushButton>

              namespace Ui {
              class FilmInput;
              }

              class FilmInput : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit FilmInput(QWidget parent = 0);
              ~FilmInput();
              void obtainFilmData(Film& f);
              void saveFilm(Film& f);
              public slots:
              void getFilm();
              private:
              Ui::FilmInput ui;
              //widgets
              QMainWindow
              window;
              QLabel
              infoLabel;
              QLabel* titleLabel;
              QLabel* durationLabel;
              QLabel* directorLabel;
              QLabel* relDateLabel;
              QTextEdit* titleEdit;
              QTextEdit* durationEdit;
              QTextEdit* directorEdit;
              QTextEdit* relDateEdit;
              QPushButton* saveBtn;
              QPushButton* cancelBtn;
              Film f;
              //sets up gui and connects signals and slots
              void setUpGui();
              };

              #endif // FILMINPUT_H

              #include "Film.h"
              #include <QDate>
              #include <QString>

              Film::Film(QString t,int dur,QString dir,QDate r):m_title(t),m_duration(dur),m_director(dir),m_releaseDate(r){

              }

              Film::Film(){
              }

              void Film::setTitle(QString t){
              m_title = t;
              }

              void Film::setDuration(int dur){
              m_duration = dur;
              }

              void Film::setDirector(QString dir){
              m_director = dir;
              }

              void Film::setReleaseDate(QDate r){
              m_releaseDate = r;
              }

              QString Film::getTitle() const{
              return QString("%1").arg(m_title);
              }

              int Film::getDuration() const{
              return m_duration;
              }
              QString Film::getDirector() const{
              return QString("%1").arg(m_director);
              }
              QDate Film::getReleaseDate() const{
              return m_releaseDate;
              }
              QString Film::toString()
              {
              return m_title + " " + m_duration + " " + m_director + " " + m_releaseDate.toString();
              }

              #include "FilmWriter.h"
              #include <QtGui>
              #include <QFileDialog>
              #include <QFile>
              #include <QMessageBox>
              #include <QObject>
              #include <QTextStream>

              void FilmWriter::saveFilm(Film& f){
              QString fileName = QFileDialog::getSaveFileName(this,("Save File"));
              if (fileName != "") {
              QFile file(fileName);
              if (!file.open(QIODevice::WriteOnly)) {
              QMessageBox::critical(this, ("Error"),("Could not open file"));// error message
              } else {
              QTextStream stream(&file);
              stream << f.toString();
              stream.flush();
              file.close();
              }
              }

              }

              #include "filminput.h"
              #include "ui_filminput.h"
              #include <QtGui>
              #include "Film.h"
              #include "FilmWriter.h"
              #include <QTextEdit>
              #include <QDate>
              #include <QString>

              FilmInput::FilmInput(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::FilmInput)
              {
              ui->setupUi(this);
              setUpGui();
              }

              FilmInput::~FilmInput()
              {
              delete ui;
              }

              void FilmInput::setUpGui(){
              //initialise widgets
              infoLabel = new QLabel("Please enter film data which will be saved to a file",this);
              titleLabel = new QLabel("Film Title",this);
              durationLabel = new QLabel("Film Duration",this);
              directorLabel = new QLabel("Film Director",this);
              relDateLabel = new QLabel("Film Release Date",this);
              titleEdit = new QTextEdit(this);
              durationEdit = new QTextEdit(this);
              directorEdit = new QTextEdit(this);
              relDateEdit = new QTextEdit(this);
              saveBtn = new QPushButton("Save Film",this);
              cancelBtn = new QPushButton("Cancel",this);
              //set layout
              QFormLayout* layout = new QFormLayout();
              layout->addWidget(infoLabel);
              layout->addWidget(titleLabel);
              layout->addWidget(titleEdit);
              layout->addWidget(durationLabel);
              layout->addWidget(durationEdit);
              layout->addWidget(directorLabel);
              layout->addWidget(directorEdit);
              layout->addWidget(relDateLabel);
              layout->addWidget(relDateEdit);
              layout->addWidget(saveBtn);
              layout->addWidget(cancelBtn);

              this->ui->widget->setLayout(layout);
              this->setWindowTitle("Film Archive");
              connect(saveBtn,SIGNAL(clicked()),this, SLOT(getFilm()));
              connect(cancelBtn,SIGNAL(clicked()),this,SLOT(close()));
              

              }

              void FilmInput::getFilm(){
              Film f1(titleEdit->toPlainText(),durationEdit->toPlainText().toInt() ,directorEdit->toPlainText(),
              QDate::fromString(relDateEdit->toPlainText(),"dd/MM/YYYY"));;
              obtainFilmData(f1);
              }

              void FilmInput::obtainFilmData(Film &f){
              saveFilm(f);
              }

              #include <QtGui/QApplication>
              #include "filminput.h"

              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);

              FilmInput w;
              w.show();
              
              return a.exec&#40;&#41;;
              

              }
              @

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                saveFilm is not a member of FilmInput.

                Besides that, please, got through the examples and the documentation of Qt, that would be beneficial to you.

                I don't want to sound rude but what you wrote here will not do for an assignment.

                Why does Film inherit QWidget ?
                Why does FilmWriter inherit Film if it's only function is to write a Film given as a parameter ?
                Why do you use a UI file if you create your interface by hand anyway ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dmon
                  wrote on last edited by
                  #9

                  I thought that FilmWriter would need to inherit Film in order to create an object of type Film. Film inheriting QWidget is a mistake as it is left over from the question which was one class called film inheriting QWidget which I have to break up into 3 classes and then add functionality. Also the UI file was created automatically for me with Qt creator. I will look up some info on interfaces. So the question is to have the saveFilm function in the filmWriter class, how do I get it to work like that rather than making saveFilm a member of FilmInput and removing the FilmWriter class?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    No it doesn't, as long as it knows how to use Film.

                    For the rest it's essentially doc reading

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dmon
                      wrote on last edited by
                      #11

                      if I take out #include "Film.h" in FilmWriter there is a problem with the FilmWriter class, if I then take out : public Film there is a problem with the func saveFilm because it's parameter is of type Film. So how do I make it know how to use Film?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        This is C/C++ basic knowledge. Without the header file how would your class know anything about Film ?

                        What problem would it be ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dmon
                          wrote on last edited by
                          #13

                          well it wouldn't know about Film without the header file. Are you saying that it doesn't need to be a child of Film but just include the header?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            Yes, it's exactly what I'm saying

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Dmon
                              wrote on last edited by
                              #15

                              Ok great. But now after the changes I am still getting the same error :-?
                              :: error: collect2: ld returned 1 exit status

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                If you are talking about the saveFilm problem and the message didn't changed, then saveFilm is still not a member of FilmInput.

                                Isn't it a member of FilmWriter ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  Dmon
                                  wrote on last edited by
                                  #17

                                  Yes and I don't want it to be a member of FilmInput, I want it to be member of FilmWriter only and somehow (I don't know how) be called in FilmInput. I thought if I included the FilmWriter header it would work, but obviously I must do something else.

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    FilmWriter is a class, how do you call a function from a class ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      Dmon
                                      wrote on last edited by
                                      #19

                                      You create an object and call the function with the object. That's it! Ok so now you have helped me to get rid of that error, thanks a lot. Another question, when I click on the save film button which invokes saveFilm the program crashes, should I start a new thread because this is a new question or can you still help me on this one?

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #20

                                        Run your application through the debugger, it will tell you where the crash occurs

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          Dmon
                                          wrote on last edited by
                                          #21

                                          I got this: The inferior stopped because it received a signal from the operating system
                                          signal name: SIGSEGV
                                          Signal meaning: Segmentation fault
                                          then it is pointing to line 2185 in qglobal.h
                                          inline QFlags operator&(Enum f) const { QFlags g; g.i = i & f; return g; }

                                          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