I am making a GUI application in which the user insert some information and click either an Add button to add more info, or a Finished button to finish the process. The outputs of the process are going to be 4 text files. So, I've made a class with 4 functions each of which creates one of those files:
class RequiredFilesMaker
{
public:
RequiredFilesMaker();
~RequiredFilesMaker();
void a1(float xcor, float ycor, int xnbin, int ynbin , int resolution);
void a2();
void a3();
void a4();
};
Due to my initial research, I understand that I should use QTextStream to save the data to a file. But, there is an issue with using these handy objects. I can't define them in the class and initialize them in the constructor. I checked all the documentations and forums to see what other people have done. But, I found out that the only method that is widely used is to call the object inside a function and close the data file after doing the job (which is not my favorite, since I need to keep the file open for the user to write on it).
QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
// writes "Result: 3.14 2.7 "
}
Is there anyone who knows how to address the issue? I came up with a new idea of saving all the information inside another variable (MyMagic) and just call a function like:
void RequiredFilesMaker::a1(float b1, float b2, int b3, int b4, int b5){
QFile gs_qfile("abc.dat");
if (!gs_qfile.open(QIODevice::WriteOnly)){
qDebug() <<"Couldn't open the file";
return;
}
QDataStream out(&gs_qfile);
out.setVersion(QDataStream::Qt_4_7);
out << MyMagic;
gs_qfile.flush();
gs_qfile.close();
But, I am wondering about the type of MyMagic.
You need a singleton class which is shared across all classes and manages all logging operations.
You can use get instance method in your application to provide a global class instance :