0

如何使用 setName/getName 方法的成员命名文件来设置 saveFile 方法的输出文件名。QString nomeFile 在 file.h 中是私有的 我创建的文件返回以下错误

QFSFileEngine::open: 未指定文件名

对话框.cpp

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

file ogg1;
ogg1.setName(nomeFile);

f.cpp

file ogg2;
ogg2.saveFile();

文件.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 << ".....";
    }
}
4

2 回答 2

1

尝试像这样使用它: setName("filename.txt");然后在您的 saveFile 方法中,添加这样的参数:a::saveFile(QString _nomefile)然后当您调用该方法时,a::saveFile(getName())

于 2016-10-17T07:51:32.287 回答
1

一些猜测如下:

您有一些类似于 dialog.hpp 的内容:

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

您在两个文件中使用了两个单独的对象,要解决此问题,您应该使用一个对象并引用它。

例如

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
}

对话框.cpp

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

f.cpp

file ogg1;
MyDialog * dialog(this, ogg1);
dialog->exec();
ogg1->saveFile();
于 2016-10-17T12:15:44.510 回答