Unsolved QTemporaryFile - can't open
-
@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 :)
-
@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 withQTemporaryFile
. -
@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.
-
@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
-
@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.
-
@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
-
@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....
-
@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.
-
@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. -
@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.
-
@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.
Make sure to add the separator after tempPath():
QString filePathAndName = QDir::tempPath() + "/myfileName.myExtension";
-
@J-Hilk Thank you. But why I get "1" and from exitStatus - NormalExit?
-
-
@J-Hilk Thank you.
Did you try run my code on your pc? Can you read from QTemporaryFile which have result of "net use'?
-
@qwe3
There are so many potential errors in your code:-
You are not running command
net use
, you are running commandcmd /c ...
. -
Personally I would not rely on/be sure of what code
cmd
might exit with. I certainly would not read anything into1
implying "crashed". -
You do not attempt to put slots on https://doc.qt.io/qt-5/qprocess.html#started, nor https://doc.qt.io/qt-5/qprocess.html#stateChanged, so I don't even know whether you know whether your process starts or not.
-
You are using
waitForFinished()
, which is not a good idea, and I believe is stated somewhere should not be used under Windows. -
You do not attempt to quote the temporary filename/path you are passing to
cmd
with the redirection. If it contains a space, your code will be wrong. -
You pass the path as a path from Qt, which uses
/
, and assume that is acceptable tocmd
for redirection. I have written about this before, and pointed out the pitfalls. If you intend to pass a Qt path on the command line to something under Windows, you should make the effort to call https://doc.qt.io/qt-5/qdir.html#toNativeSeparators, at least in my opinion. -
As I said earlier, if you really think
QTemporaryFile
is what is causing your problem because it works OK with a file you create instead, why don't you do that instead of insisting onQTemporaryFile
? @J-Hilk also suggested this earlier. There is nothing magic inQTemporaryFile
.
I would like to don't write any path to File and delete that file when it finished job.
I have no idea what you intend by "I would like to don't write any path to File ". If you do not pass a full path to the file to your command, you are asking for trouble. You can delete a file when you're done.
-