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. QTemporaryFile - can't open
Forum Updated to NodeBB v4.3 + New Features

QTemporaryFile - can't open

Scheduled Pinned Locked Moved Unsolved General and Desktop
38 Posts 4 Posters 4.2k 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by
    #18

    @jsulm Ok, I close temp file before starting process.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        tmpFile = new QTemporaryFile;
        tmpFile->open();
        proc = new QProcess;
        tmpFile->close();
        connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
        connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
        proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
        proc->waitForFinished();
    
        qInfo()<<proc->exitCode();
    
        qInfo()<<tmpFile->fileName();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
    {
        qInfo()<<"error"<<error;
    }
    
    void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
    {
        qInfo()<<"exitCode in Slot"<<exitCode;
        qInfo()<<"exit Status in Slot"<<exitStatus;
    }
    
    

    qInfo says:
    exitCode in Slot 1
    exit Status in Slot QProcess::NormalExit
    1

    jsulmJ 1 Reply Last reply
    0
    • Q qwe3

      @jsulm Ok, I close temp file before starting process.

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QDebug>
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          tmpFile = new QTemporaryFile;
          tmpFile->open();
          proc = new QProcess;
          tmpFile->close();
          connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
          connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
          proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
          proc->waitForFinished();
      
          qInfo()<<proc->exitCode();
      
          qInfo()<<tmpFile->fileName();
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
      {
          qInfo()<<"error"<<error;
      }
      
      void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
      {
          qInfo()<<"exitCode in Slot"<<exitCode;
          qInfo()<<"exit Status in Slot"<<exitStatus;
      }
      
      

      qInfo says:
      exitCode in Slot 1
      exit Status in Slot QProcess::NormalExit
      1

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #19

      @qwe3 "but I have to write data to file and from file get data to QTextStream" - you do not need a file to use QTextStream, I still don't understand why you need a file... It would be way easier to get the output from the command without messing up with temp file.

      QProcess proc;
      QString cmdCommand = "cmd";
      QStringList param;
      param << "/c" << "net" << "use";
      proc.start(cmdCommand, param);
      proc.waitForFinished();
      QTextStream stream(proc.readAllStandardOutput());
      

      Does your current code write the file or not?

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

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        qwe3
        wrote on last edited by qwe3
        #20

        @jsulm Ok, so the problem is that I can't use QProcess. I have to use winApi CreateProcess, which is a little different. And in this CreateProcess the new process will be have shell parent, so this is very strange. I know. That new process will be execute cmdCommand. I know that can be problem with ( waitForFinished, but at this moment is not a problem ). So I can't use readAllStandardOutput. I have to write data to File. This is only way. So now i would like know why this code with QTemporaryFile and QProcess doesn't work. Of course QProcess works when I use it with file which is on the Desktop or will be created.

        I tested CreateProcess from WinApi and it works, but I have to use QTemporaryFile. So I started from QTemporaryFile and QProcess.

        EDIT my last code which is here: there is no data on temporary File.

        jsulmJ 1 Reply Last reply
        0
        • Q qwe3

          @jsulm Ok, so the problem is that I can't use QProcess. I have to use winApi CreateProcess, which is a little different. And in this CreateProcess the new process will be have shell parent, so this is very strange. I know. That new process will be execute cmdCommand. I know that can be problem with ( waitForFinished, but at this moment is not a problem ). So I can't use readAllStandardOutput. I have to write data to File. This is only way. So now i would like know why this code with QTemporaryFile and QProcess doesn't work. Of course QProcess works when I use it with file which is on the Desktop or will be created.

          I tested CreateProcess from WinApi and it works, but I have to use QTemporaryFile. So I started from QTemporaryFile and QProcess.

          EDIT my last code which is here: there is no data on temporary File.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #21

          @qwe3 I'm completely lost now: why do you think you can't use QProcess?
          Did you actually try the code I provided?
          net is writing to stdout as far as I know (maybe stderr, but that you also can read using QProcess), so using readAllStandardOutput() should give you output from net without any files.

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

          1 Reply Last reply
          2
          • Q Offline
            Q Offline
            qwe3
            wrote on last edited by qwe3
            #22

            @jsulm Because my app is with administrator privileges. So when I do something like:
            process->start("cmd /c net use Z: \192.168.24.12\folderName") I mapped disk to administator Files and I don't see it on myComputer. I read about it on other forum. Because cmd will be has admin privileges too. So I have to create Process with no admin privileges. And createProcess from winApi can do something like that.

            Yes, when I use readAllStandardOutput and I don't use temporaryFile everything is ok. But I have to use temporaryFile.

            EDIT and I want to execute "net use", which give me all drivers mapped to my account ( when I execute this cmd command with admin privileges I get all drivers mapped to admin ).

            JonBJ 1 Reply Last reply
            0
            • Q qwe3

              @jsulm Because my app is with administrator privileges. So when I do something like:
              process->start("cmd /c net use Z: \192.168.24.12\folderName") I mapped disk to administator Files and I don't see it on myComputer. I read about it on other forum. Because cmd will be has admin privileges too. So I have to create Process with no admin privileges. And createProcess from winApi can do something like that.

              Yes, when I use readAllStandardOutput and I don't use temporaryFile everything is ok. But I have to use temporaryFile.

              EDIT and I want to execute "net use", which give me all drivers mapped to my account ( when I execute this cmd command with admin privileges I get all drivers mapped to admin ).

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #23

              @qwe3
              I too am lost, but why do you think a temporary file created in one process with certain privileges/in a certain location will be accessible/writeable by another process with different privileges? (I think that's what you're doing, you have shown various different code.)

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qwe3
                wrote on last edited by
                #24

                @JonB I can create file with createProcess and access to it ( for example read ) from my Main App with admin privileges. I check that. But I want only to use temporary File - no file which is on Desktop. I think the information about winapi and createProcess is not important. Now I only would like to create Temporary File, write to it some data using QProcess and qDebug() this data from file. But I don't want use readAllStandardOutput. Why? When I can do this -> write data to file and read it I can do the same with CreateProcess and QTemporaryFile. So please forgot about winapi :)

                JonBJ 1 Reply Last reply
                0
                • Q qwe3

                  @JonB I can create file with createProcess and access to it ( for example read ) from my Main App with admin privileges. I check that. But I want only to use temporary File - no file which is on Desktop. I think the information about winapi and createProcess is not important. Now I only would like to create Temporary File, write to it some data using QProcess and qDebug() this data from file. But I don't want use readAllStandardOutput. Why? When I can do this -> write data to file and read it I can do the same with CreateProcess and QTemporaryFile. So please forgot about winapi :)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #25

                  @qwe3
                  I never said anything about "winapi". It is you who say something about the sub-process is running with different permissions from the parent process which creates the file, that's all I'm talking about.

                  I think also you are running cmd /c something > tempfile. Does it help instead if you try https://doc.qt.io/qt-5/qprocess.html#setStandardOutputFile, so you don't have to do the redirection in the command?

                  Finally, instead of using QTemporaryFile, what happens if you (temporarily) try a fixed, non-temporary file path? To eliminate whether question has anything to do with QTemporaryFile.

                  1 Reply Last reply
                  1
                  • Q Offline
                    Q Offline
                    qwe3
                    wrote on last edited by
                    #26

                    @JonB I would like to don't write any path to File and delete that file when it finished job. QTemporaryFile do exactly what I want - no Path and setAutoRemove.

                    J.HilkJ JonBJ 2 Replies Last reply
                    0
                    • Q qwe3

                      @JonB I would like to don't write any path to File and delete that file when it finished job. QTemporaryFile do exactly what I want - no Path and setAutoRemove.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #27

                      @qwe3 are you sure about the life time of your QTemporaryFile object ? because the file will be deleted when the object ist destroyed. Thats one of THE features QTemporaryFile has

                      https://doc.qt.io/qt-5/qtemporaryfile.html#details

                      and the file will subsequently be removed upon destruction of the QTemporaryFile object


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      2
                      • Q Offline
                        Q Offline
                        qwe3
                        wrote on last edited by
                        #28

                        @J-Hilk
                        But in my code:

                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        #include <QDebug>
                        MainWindow::MainWindow(QWidget *parent)
                            : QMainWindow(parent)
                            , ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                        
                            tmpFile = new QTemporaryFile;
                            tmpFile->open();
                            proc = new QProcess;
                            tmpFile->close();
                            connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                            connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                            proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                            proc->waitForFinished();
                        
                            qInfo()<<proc->exitCode();
                        
                            qInfo()<<tmpFile->fileName();
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        
                        void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                        {
                            qInfo()<<"error"<<error;
                        }
                        
                        void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                        {
                            qInfo()<<"exitCode in Slot"<<exitCode;
                            qInfo()<<"exit Status in Slot"<<exitStatus;
                        }
                        

                        I don't destroy temporary File, so I can go to Windows Explorer and open this file. When I open it, it really exists ( when I change one letter there is error "that file not exists" ). But temporary File is empty.

                        J.HilkJ 2 Replies Last reply
                        0
                        • Q qwe3

                          @J-Hilk
                          But in my code:

                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          #include <QDebug>
                          MainWindow::MainWindow(QWidget *parent)
                              : QMainWindow(parent)
                              , ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                          
                              tmpFile = new QTemporaryFile;
                              tmpFile->open();
                              proc = new QProcess;
                              tmpFile->close();
                              connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                              connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                              proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                              proc->waitForFinished();
                          
                              qInfo()<<proc->exitCode();
                          
                              qInfo()<<tmpFile->fileName();
                          }
                          
                          MainWindow::~MainWindow()
                          {
                              delete ui;
                          }
                          
                          void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                          {
                              qInfo()<<"error"<<error;
                          }
                          
                          void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                          {
                              qInfo()<<"exitCode in Slot"<<exitCode;
                              qInfo()<<"exit Status in Slot"<<exitStatus;
                          }
                          

                          I don't destroy temporary File, so I can go to Windows Explorer and open this file. When I open it, it really exists ( when I change one letter there is error "that file not exists" ). But temporary File is empty.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #29

                          @qwe3 And you say with File it works ? Than QTemprorayFile is the wrong approach here.

                          if you control both programs, than there are other better options to transfer information between those programs


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          0
                          • Q qwe3

                            @JonB I would like to don't write any path to File and delete that file when it finished job. QTemporaryFile do exactly what I want - no Path and setAutoRemove.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #30

                            @qwe3 said in QTemporaryFile - can't open:

                            @JonB I would like to don't write any path to File and delete that file when it finished job. QTemporaryFile do exactly what I want - no Path and setAutoRemove.

                            I know that. I wrote

                            what happens if you (temporarily) try
                            To eliminate whether question has anything to do with QTemporaryFile.

                            When people suggest you try something in order to find out what's going in case it resolves or clarifies a problem, it's usual to give it a try rather than refusing to do so as it's not what you want at the end....

                            1 Reply Last reply
                            0
                            • Q Offline
                              Q Offline
                              qwe3
                              wrote on last edited by
                              #31

                              @J-Hilk When I change QTemporaryFile to QFile which is on my Desktop - everything is ok.

                              @JonB My English is not good, so I don't understand everything what you say. Sorry for that. You say about setStandardOutputFile, but when I check this function I find out that has to QString fileName. This is not good for me. When I change in my code QTemporaryFile to QFile and add path to that File I have an equivalent to setStandardOutputFile. So I look for only temporary files way.

                              1 Reply Last reply
                              0
                              • Q qwe3

                                @J-Hilk
                                But in my code:

                                #include "mainwindow.h"
                                #include "ui_mainwindow.h"
                                #include <QDebug>
                                MainWindow::MainWindow(QWidget *parent)
                                    : QMainWindow(parent)
                                    , ui(new Ui::MainWindow)
                                {
                                    ui->setupUi(this);
                                
                                    tmpFile = new QTemporaryFile;
                                    tmpFile->open();
                                    proc = new QProcess;
                                    tmpFile->close();
                                    connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                    connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                                    proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                                    proc->waitForFinished();
                                
                                    qInfo()<<proc->exitCode();
                                
                                    qInfo()<<tmpFile->fileName();
                                }
                                
                                MainWindow::~MainWindow()
                                {
                                    delete ui;
                                }
                                
                                void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                {
                                    qInfo()<<"error"<<error;
                                }
                                
                                void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                                {
                                    qInfo()<<"exitCode in Slot"<<exitCode;
                                    qInfo()<<"exit Status in Slot"<<exitStatus;
                                }
                                

                                I don't destroy temporary File, so I can go to Windows Explorer and open this file. When I open it, it really exists ( when I change one letter there is error "that file not exists" ). But temporary File is empty.

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #32

                                @qwe3 said in QTemporaryFile - can't open:

                                I don't destroy temporary File, so I can go to Windows Explorer and open this file. When I open it, it really exists ( when I change one letter there is error "that file not exists" ). But temporary File is empty.

                                I'm surprised that your file manager lets you open it in the first place, because according to the documentation

                                For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.

                                So read access you have, write access do not have. anyway QTemporaryFile not the class to use here.

                                If you insist on some form of QFile usage, use QDir::tempPath() to locate where QTemporaryFile would have stored the file. That way its "not on the desktop" what ever that even means.


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                1
                                • Q Offline
                                  Q Offline
                                  qwe3
                                  wrote on last edited by qwe3
                                  #33

                                  @J-Hilk I don't know what means "1" when I execute: qInfo()<<proc->exitCode();

                                  When I use QFile on desktop i get "0", but exitStatus is the same.

                                  J.HilkJ 1 Reply Last reply
                                  0
                                  • Q qwe3

                                    @J-Hilk I don't know what means "1" when I execute: qInfo()<<proc->exitCode();

                                    When I use QFile on desktop i get "0", but exitStatus is the same.

                                    J.HilkJ Offline
                                    J.HilkJ Offline
                                    J.Hilk
                                    Moderators
                                    wrote on last edited by
                                    #34

                                    @qwe3 said in QTemporaryFile - can't open:

                                    @J-Hilk I don't know what means "1" when I execute: qInfo()<<proc->exitCode();

                                    When I use QFile on desktop i get "0", but exitStatus is the same.

                                    1e505e78-d78b-47d8-affe-b1ced4433264-image.png

                                    Make sure to add the separator after tempPath():

                                    QString filePathAndName = QDir::tempPath() + "/myfileName.myExtension";


                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    1 Reply Last reply
                                    0
                                    • Q Offline
                                      Q Offline
                                      qwe3
                                      wrote on last edited by
                                      #35

                                      @J-Hilk Thank you. But why I get "1" and from exitStatus - NormalExit?

                                      J.HilkJ 1 Reply Last reply
                                      0
                                      • Q qwe3

                                        @J-Hilk Thank you. But why I get "1" and from exitStatus - NormalExit?

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #36

                                        @qwe3 45c2ea02-44ed-4d7b-9f62-9d35c39ad423-image.png


                                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                        Q: What's that?
                                        A: It's blue light.
                                        Q: What does it do?
                                        A: It turns blue.

                                        1 Reply Last reply
                                        1
                                        • Q Offline
                                          Q Offline
                                          qwe3
                                          wrote on last edited by
                                          #37

                                          @J-Hilk Thank you.

                                          Did you try run my code on your pc? Can you read from QTemporaryFile which have result of "net use'?

                                          JonBJ 1 Reply Last reply
                                          0

                                          • Login

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