0

how can i use the member nomeFile of setName/getName methods to set the output file name of saveFile method. QString nomeFile is private inside file.h The file i created returns the following error

QFSFileEngine::open: No file name specified

dialog.cpp

nomeFile="abcd"; // private: QString nomeFile; in dialog.h

file ogg1;
ogg1.setName(nomeFile);

f.cpp

file ogg2;
ogg2.saveFile();

file.cpp

/* COSTRUTTORE */
a::a()
{

}

/* DISTRUTTORE */
a::~a()
{

}

void a::setName(QString _nomeFile)
{
    nomeFile="C:\\Users\\MDN\\Documents\\A\\" + _nomeFile + ".txt";
    if(!nomeFile.isEmpty())         
    {
        QFile::remove(nomeFile);    
    }
}

QString a::getName()
{
    return nomeFile;
}

void a::saveFile()
{
    QFile file(nomeFile);
    if (file.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text)
    {
      QTextStream stream(&file);
      stream << "File salvato correttamente";
      stream << ".....";
      stream << ".....";
    }
}
Massimo D. N.
  • 316
  • 1
  • 6
  • 17
  • You do call `a::setName` before calling `a::saveFile`? – Some programmer dude Oct 17 '16 at 07:40
  • Also, you *are* using the member variable correctly otherwise. If you didn't then you would have a *compiler* error, and not a *run-time* error (i.e. you would get an error from the compiler when trying to build the program, not getting an error in the console when trying to run the program). – Some programmer dude Oct 17 '16 at 07:41
  • @ Joachim Yes, you are right. I setted the name in the dialog.cpp and called the save in another Class. Trying to correct the code. Thank you – Massimo D. N. Oct 17 '16 at 08:31
  • i must initialize setName(nomeFile); in dialog.cpp and then saveFile in f.cpp – Massimo D. N. Oct 17 '16 at 09:11
  • Am I thinking too simply? I see a class with some attribute of which you instantiate two objects. One object, ogg1, you give a file name, the other one, ogg2, you try to save, despite having given it no file name. Or is the file name attribute intended to be static? Can you give us the head of your file class? – Aziuth Oct 17 '16 at 09:20
  • @ Azimuth How can i pass nomeFile, created inside void a::setName(QString _nomeFile) to ogg2.saveFile() created inside the other class f? My original project is composed of 6 different Classes (6 .cpp files). PS i'm new in QT and my english is not so good – Massimo D. N. Oct 17 '16 at 09:38
  • @Aziuth I see that too. OP seems to be confusing class with object. – Caleth Oct 17 '16 at 12:08
  • @MassimoD.N. Please respond by my actual user name, if not, I won't see it popping up. That said, Caleth seems to be right, are you sure that you know what a class is? A .cpp file is not a class. I asked for the header file, and you seem not to know what that is. We can go on and on about your problem and even post code that solves it, but if Caleth and I are correct, that won't help you. You need basic C++ tutorials. Not QT, basic C++. Like this one: http://www.cprogramming.com/tutorial/c++-tutorial.html – Aziuth Oct 18 '16 at 09:30
  • @Aziuth I'm trying to use MegaColorBoy solution, probably the more easy for me. I'm testing it and for now it seems to work. I would like to post the project files but are 12 files plus other ten text files that are read from the project. – Massimo D. N. Oct 18 '16 at 13:31
  • @MassimoD.N. Then you create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and give that to us. But again, I cannot stress this enough, you need to understand what your code does, every single bit. You need to understand how classes work. At this point, I don't think you do, might be that I'm wrong, but if I'm not, you should read the basics before you continue. There are bugs that won't show right away or might be invisible at all which can cost you a lot of nerve later on. – Aziuth Oct 19 '16 at 09:27
  • Here is the link to complete project [link]http://www.mediafire.com/file/b13jnp4niwi4ltv/Ericsson_V_3_02.rar The project read a text file from 1319 folder. This file is for mount electronic components on a board, so there are position, part number, rotation, x and y coordinates and a byz that contains component dimentions and other info. In the 1319 file, the coordinates are referred to the pin 1 of the component while the mounting machine wants them on the center. The program modify the coord from pin 1 to center. If the byz is missing the operator can insert it. – Massimo D. N. Oct 19 '16 at 09:45
  • 1
    @MassimoD.N. You might want to click on my link above. – Aziuth Oct 20 '16 at 08:53
  • @Aziuth do you mean this http://www.cprogramming.com/tutorial/c++-tutorial.html ? – Massimo D. N. Oct 20 '16 at 12:28
  • @MassimoD.N. No, I meant the link to the [Minimal, Complete and Varifiable Example](http://stackoverflow.com/help/mcve). You just posted the opposite. You can be assured that nobody will look through a complete project. It's your job to break it down for us. – Aziuth Oct 21 '16 at 08:47

2 Answers2

1

try using it like this: setName("filename.txt"); and then in your saveFile method, add a parameter like this: a::saveFile(QString _nomefile) and then when you call the method, a::saveFile(getName())

1

Some guesswork follows:

You have something along the lines of dialog.hpp:

class MyDialog : QDialog
{
public:
    MyDialog(QObject * parent = 0) : QDialog(parent) {}
    // other public stuff here
private:
    QString nomeFile;
    // other private stuff here
}

You are using two separate objects in your two files, to fix this you should use one object, and reference it.

e.g.

class MyDialog : QDialog
{
public:
    MyDialog(QObject * parent = 0, file& ogg1) : QDialog(parent), m_ogg1(ogg1) {}
    // other public stuff here
private:
    QString nomeFile;
    file& m_ogg1;
    // other private stuff here
}

dialog.cpp

void MyDialog::someMethod()
{
    nomeFile="abcd"; 
    m_ogg1.setName(nomeFile);
}

f.cpp

file ogg1;
MyDialog * dialog(this, ogg1);
dialog->exec();
ogg1->saveFile();
Caleth
  • 52,200
  • 2
  • 44
  • 75