Problem with saving function (overwrite file which saved by dialog)
- 
Okay, so this is your function: void MainWindow::mySaveFunction() const { QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); QFile file; file.setFileName(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Fehler beim Speichern der Datei"; } QTextStream out(&file); out << toHtml(); file.close(); }I guessed it's a member function of something, let's assume this something is MainWindow. Next guess istoHtmlis another member function. So, the following line does nothing:QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);And what about fileName? Is that a member variable?@Wieland 
 toHtml() is QString
 fileName is QStringAnd yes, the function is of QMainWindow 
- 
Okay, so this is your function: void MainWindow::mySaveFunction() const { QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); QFile file; file.setFileName(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Fehler beim Speichern der Datei"; } QTextStream out(&file); out << toHtml(); file.close(); }I guessed it's a member function of something, let's assume this something is MainWindow. Next guess istoHtmlis another member function. So, the following line does nothing:QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);And what about fileName? Is that a member variable?@Wieland 
 void MainWindow::mySaveFunction()without const 
- 
Maybe your fileName is wrong? Try this: if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << QString("Fehler beim Speichern der Datei: %1").arg(fileName); }
- 
Maybe your fileName is wrong? Try this: if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << QString("Fehler beim Speichern der Datei: %1").arg(fileName); }@Wieland QFSFileEngine::open: No file name specified "Fehler beim Speichern der Datei: " QIODevice::write (QFile, ""): device not open
- 
@Wieland QFSFileEngine::open: No file name specified "Fehler beim Speichern der Datei: " QIODevice::write (QFile, ""): device not open@HenrikSt. Congrats, you just found your problem, fileName is empty :-) 
- 
That good, i know.. But i don't know how to fix. 
 If you help me I am very happy :-)
- 
That good, i know.. But i don't know how to fix. 
 If you help me I am very happy :-)You must assign some useful value to fileName, e.g. like this: void MainWindow::mySaveFunction() { const QString path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); QFile file; fileName = QString("%1/%2").arg(path).arg("unbenannt.html"); file.setFileName(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << QString("Fehler beim Speichern der Datei: %1").arg(fileName); } QTextStream out(&file); out << toHtml(); file.close(); }
- 
You must assign some useful value to fileName, e.g. like this: void MainWindow::mySaveFunction() { const QString path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); QFile file; fileName = QString("%1/%2").arg(path).arg("unbenannt.html"); file.setFileName(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << QString("Fehler beim Speichern der Datei: %1").arg(fileName); } QTextStream out(&file); out << toHtml(); file.close(); }@Wieland 
 Hi, this works, but the Problem is, that you create a new file ("unbenannt.html")I want to overwrite the file i create with the Dialog before. You understand? 
- 
@Wieland 
 Hi, this works, but the Problem is, that you create a new file ("unbenannt.html")I want to overwrite the file i create with the Dialog before. You understand? @HenrikSt. In this case the name you assign to fileName here must exactly match the name of the existing file. 
- 
@HenrikSt. In this case the name you assign to fileName here must exactly match the name of the existing file. @Wieland 
 I know... But every user enter another fileName so "unbenannt.htm" will not work..Do you have another idea? Thanks :) 
- 
@Wieland 
 I know... But every user enter another fileName so "unbenannt.htm" will not work..Do you have another idea? Thanks :) @HenrikSt. After the user entered the file name the first time, just save the name then and use it again later. 
- 
@HenrikSt. After the user entered the file name the first time, just save the name then and use it again later. @Wieland 
 Ah ok. I'm new in Qt. Can you write an example for this case that i can use it?I have Problems with writing Code. Reading is no Problem. Do you have any tips for me? 
- 
@Wieland 
 Ah ok. I'm new in Qt. Can you write an example for this case that i can use it?I have Problems with writing Code. Reading is no Problem. Do you have any tips for me? The following code isn't very elegant but I think it's good enough to serve as an example. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); bool mySaveFunction(); QString toHtml() const; private slots: void saveFile(); private: Ui::MainWindow *ui; QString m_fileName; }; #endif // MAINWINDOW_Hmainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> #include <QDebug> #include <QStandardPaths> #include <QFileDialog> #include <QMessageBox> bool MainWindow::mySaveFunction() { // if this happens we have a bug in our code Q_ASSERT_X(!m_fileName.isEmpty(), "mySaveFunction", "m_fileName is empty"); QFile file(m_fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::critical(this, "Fehler", QString("Fehler beim Speichern der Datei: %1").arg(m_fileName) ); return false; } QTextStream out(&file); out << toHtml(); file.close(); QMessageBox::information(this, "Information", QString("Die Datei wurde gespeichert: %1").arg(m_fileName) ); return true; } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(saveFile())); } MainWindow::~MainWindow() { delete ui; } QString MainWindow::toHtml() const { return "<html></html>\n"; } void MainWindow::saveFile() { if (m_fileName.isEmpty()) { // first attempt to save the file m_fileName = QFileDialog::getSaveFileName(this, tr("Speichern unter..."), QString("Unbenannt.htm"), tr("TextPad 1.0 (*.htm);;")); if (m_fileName.isEmpty()) { return; // dialog was canceled } } if (!mySaveFunction()) { m_fileName.clear(); // saving failed. clear m_fileName so the QFileDialog will come up again next time } }
- 
The following code isn't very elegant but I think it's good enough to serve as an example. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); bool mySaveFunction(); QString toHtml() const; private slots: void saveFile(); private: Ui::MainWindow *ui; QString m_fileName; }; #endif // MAINWINDOW_Hmainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> #include <QDebug> #include <QStandardPaths> #include <QFileDialog> #include <QMessageBox> bool MainWindow::mySaveFunction() { // if this happens we have a bug in our code Q_ASSERT_X(!m_fileName.isEmpty(), "mySaveFunction", "m_fileName is empty"); QFile file(m_fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::critical(this, "Fehler", QString("Fehler beim Speichern der Datei: %1").arg(m_fileName) ); return false; } QTextStream out(&file); out << toHtml(); file.close(); QMessageBox::information(this, "Information", QString("Die Datei wurde gespeichert: %1").arg(m_fileName) ); return true; } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(saveFile())); } MainWindow::~MainWindow() { delete ui; } QString MainWindow::toHtml() const { return "<html></html>\n"; } void MainWindow::saveFile() { if (m_fileName.isEmpty()) { // first attempt to save the file m_fileName = QFileDialog::getSaveFileName(this, tr("Speichern unter..."), QString("Unbenannt.htm"), tr("TextPad 1.0 (*.htm);;")); if (m_fileName.isEmpty()) { return; // dialog was canceled } } if (!mySaveFunction()) { m_fileName.clear(); // saving failed. clear m_fileName so the QFileDialog will come up again next time } }@Wieland 
 Hi, thank you for the exampleif (!mySaveFunction()) { m_fileName.clear(); // saving failed. clear m_fileName so the QFileDialog will come up again next time }This Code is not working... C:\Users\Henrik\Documents\TextPad\pagestextedit.cpp:557: Fehler: could not convert 'PagesTextEdit::saveUnderDocument()' from 'void' to 'bool' if (!saveUnderDocument()) { ^I have void MainWindow::save.... 
 and not boolHow can i Change it? One Problem is existing.. 
 When i open a file e.g. from Desktop and i write something in and want to save it with mySaveFunction i get an runtime error...
 Do you know why?Thanks a lot 
- 
@Wieland 
 Hi, thank you for the exampleif (!mySaveFunction()) { m_fileName.clear(); // saving failed. clear m_fileName so the QFileDialog will come up again next time }This Code is not working... C:\Users\Henrik\Documents\TextPad\pagestextedit.cpp:557: Fehler: could not convert 'PagesTextEdit::saveUnderDocument()' from 'void' to 'bool' if (!saveUnderDocument()) { ^I have void MainWindow::save.... 
 and not boolHow can i Change it? One Problem is existing.. 
 When i open a file e.g. from Desktop and i write something in and want to save it with mySaveFunction i get an runtime error...
 Do you know why?Thanks a lot @HenrikSt. 
 The Code which is not working i fixed it...But the other Problem not 
- 
@Wieland 
 Hi, thank you for the exampleif (!mySaveFunction()) { m_fileName.clear(); // saving failed. clear m_fileName so the QFileDialog will come up again next time }This Code is not working... C:\Users\Henrik\Documents\TextPad\pagestextedit.cpp:557: Fehler: could not convert 'PagesTextEdit::saveUnderDocument()' from 'void' to 'bool' if (!saveUnderDocument()) { ^I have void MainWindow::save.... 
 and not boolHow can i Change it? One Problem is existing.. 
 When i open a file e.g. from Desktop and i write something in and want to save it with mySaveFunction i get an runtime error...
 Do you know why?Thanks a lot @HenrikSt. said: Do you know why? No. 
- 
@HenrikSt. said: Do you know why? No. 
- 
@HenrikSt. 
 But you understand my Problem?
- 
The cause for that crash can be everything! 
 How should anybody know what it is without your code?
 Did you try to debug your program to see where it crashes?
 
