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. Problem with saving function (overwrite file which saved by dialog)

Problem with saving function (overwrite file which saved by dialog)

Scheduled Pinned Locked Moved Solved General and Desktop
saveqtexteditfilefunction
25 Posts 3 Posters 9.9k 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.
  • ? A Former User
    22 May 2016, 16:16

    Maybe your fileName is wrong? Try this:

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
      qDebug() << QString("Fehler beim Speichern der Datei: %1").arg(fileName);
    }
    
    ? Offline
    ? Offline
    A Former User
    wrote on 22 May 2016, 16:46 last edited by
    #10

    @Wieland

    QFSFileEngine::open: No file name specified
    "Fehler beim Speichern der Datei: "
    QIODevice::write (QFile, ""): device not open
    
    ? 1 Reply Last reply 22 May 2016, 16:56
    0
    • ? A Former User
      22 May 2016, 16:46

      @Wieland

      QFSFileEngine::open: No file name specified
      "Fehler beim Speichern der Datei: "
      QIODevice::write (QFile, ""): device not open
      
      ? Offline
      ? Offline
      A Former User
      wrote on 22 May 2016, 16:56 last edited by
      #11

      @HenrikSt. Congrats, you just found your problem, fileName is empty :-)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 22 May 2016, 16:57 last edited by
        #12

        That good, i know..

        But i don't know how to fix.
        If you help me I am very happy :-)

        ? 1 Reply Last reply 22 May 2016, 17:04
        0
        • ? A Former User
          22 May 2016, 16:57

          That good, i know..

          But i don't know how to fix.
          If you help me I am very happy :-)

          ? Offline
          ? Offline
          A Former User
          wrote on 22 May 2016, 17:04 last edited by
          #13

          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();
          }
          
          ? 1 Reply Last reply 22 May 2016, 18:20
          1
          • ? A Former User
            22 May 2016, 17:04

            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();
            }
            
            ? Offline
            ? Offline
            A Former User
            wrote on 22 May 2016, 18:20 last edited by
            #14

            @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?

            ? 1 Reply Last reply 22 May 2016, 18:46
            0
            • ? A Former User
              22 May 2016, 18:20

              @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?

              ? Offline
              ? Offline
              A Former User
              wrote on 22 May 2016, 18:46 last edited by
              #15

              @HenrikSt. In this case the name you assign to fileName here must exactly match the name of the existing file.

              ? 1 Reply Last reply 22 May 2016, 18:53
              0
              • ? A Former User
                22 May 2016, 18:46

                @HenrikSt. In this case the name you assign to fileName here must exactly match the name of the existing file.

                ? Offline
                ? Offline
                A Former User
                wrote on 22 May 2016, 18:53 last edited by
                #16

                @Wieland
                I know... But every user enter another fileName so "unbenannt.htm" will not work..

                Do you have another idea?

                Thanks :)

                ? 1 Reply Last reply 22 May 2016, 19:15
                0
                • ? A Former User
                  22 May 2016, 18:53

                  @Wieland
                  I know... But every user enter another fileName so "unbenannt.htm" will not work..

                  Do you have another idea?

                  Thanks :)

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 22 May 2016, 19:15 last edited by
                  #17

                  @HenrikSt. After the user entered the file name the first time, just save the name then and use it again later.

                  ? 1 Reply Last reply 22 May 2016, 19:42
                  0
                  • ? A Former User
                    22 May 2016, 19:15

                    @HenrikSt. After the user entered the file name the first time, just save the name then and use it again later.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on 22 May 2016, 19:42 last edited by
                    #18

                    @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?

                    ? 1 Reply Last reply 22 May 2016, 20:25
                    0
                    • ? A Former User
                      22 May 2016, 19:42

                      @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?

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on 22 May 2016, 20:25 last edited by
                      #19

                      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_H
                      

                      mainwindow.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
                          }
                      }
                      
                      ? 1 Reply Last reply 23 May 2016, 11:49
                      1
                      • ? A Former User
                        22 May 2016, 20:25

                        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_H
                        

                        mainwindow.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
                            }
                        }
                        
                        ? Offline
                        ? Offline
                        A Former User
                        wrote on 23 May 2016, 11:49 last edited by
                        #20

                        @Wieland
                        Hi, thank you for the example

                        if (!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 bool

                        How 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

                        ? ? 2 Replies Last reply 23 May 2016, 11:53
                        0
                        • ? A Former User
                          23 May 2016, 11:49

                          @Wieland
                          Hi, thank you for the example

                          if (!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 bool

                          How 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

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on 23 May 2016, 11:53 last edited by
                          #21

                          @HenrikSt.
                          The Code which is not working i fixed it...

                          But the other Problem not

                          1 Reply Last reply
                          0
                          • ? A Former User
                            23 May 2016, 11:49

                            @Wieland
                            Hi, thank you for the example

                            if (!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 bool

                            How 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

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on 23 May 2016, 12:28 last edited by
                            #22

                            @HenrikSt. said:

                            Do you know why?

                            No.

                            ? 1 Reply Last reply 23 May 2016, 12:34
                            0
                            • ? A Former User
                              23 May 2016, 12:28

                              @HenrikSt. said:

                              Do you know why?

                              No.

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on 23 May 2016, 12:34 last edited by
                              #23

                              @Wieland
                              https://onedrive.live.com/redir?resid=8CA2253BB1C9EA6!60243&authkey=!ABJFjbW9wpRomkw&v=3&ithint=photo%2Cpng

                              ? 1 Reply Last reply 23 May 2016, 12:34
                              0
                              • ? A Former User
                                23 May 2016, 12:34

                                @Wieland
                                https://onedrive.live.com/redir?resid=8CA2253BB1C9EA6!60243&authkey=!ABJFjbW9wpRomkw&v=3&ithint=photo%2Cpng

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on 23 May 2016, 12:34 last edited by
                                #24

                                @HenrikSt.
                                But you understand my Problem?

                                1 Reply Last reply
                                0
                                • jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 23 May 2016, 12:53 last edited by
                                  #25

                                  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?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0

                                  19/25

                                  22 May 2016, 20:25

                                  • Login

                                  • Login or register to search.
                                  19 out of 25
                                  • First post
                                    19/25
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved