QProcess and shell : Destroyed while process is still running - qt

I want to launch a shell script with Qt.
QProcess process;
process.start(commandLine, QStringList() << confFile);
process.waitForFinished();
if(process.exitCode()!=0)
{
qDebug () << " Error " << process.exitCode() << process.readAllStrandardError();
}
else
{
qDebug () << " Ok " << process.readAllStrandardOutput() << process.readAllStrandardError();
}
The result is :
Ok : Result.... " "" QProcess : Destroyed while process is still
running.
This message does not appear every time.
What is the problem?

process.waitForFinished(); is hitting the default 30 seconds timeout. Use process.waitForFinished(-1); instead. This will make sure you wait for however long it takes for the process to finish, without any timeout.

Note you create QProcess into the local scope. This means that the object will be deleted when you exit the scope. In the destructor QProcess process terminates. The message "Destroyed" while "the process is still running" when the process terminates in the destructor.
For solving this problem, you should call QProcess destructor when process is already terminated.
If will be QProcess::waitForFinished(-1) into your example, it will occur, but this will block you application.

Related

SCP always returns the same error code

I have a problem copying files with scp. I use Qt and copy my files with scp using QProcess. And when something bad happens I always get exitCode=1. It always returns 1. I tried copying files with a terminal. The first time I got the error "Permission denied" and the exit code was 1. Then I unplugged my Ethernet cable and got the error "Network is unreachable". And the return code was still 1. It confuses me very much cause in my application I have to distinct these types of errors.
Any help is appreciated. Thank you so much!
See this code as a working example:
bool Utility::untarScript(QString filename, QString& statusMessages)
{
// Untar tar-bzip2 file, only extract script to temp-folder
QProcess tar;
QStringList arguments;
arguments << "-xvjf";
arguments << filename;
arguments << "-C";
arguments << QDir::tempPath();
arguments << "--strip-components=1";
arguments << "--wildcards";
arguments << "*/folder.*";
// tar -xjf $file -C $tmpDir --strip-components=1 --wildcards
tar.start("tar", arguments);
// Wait for tar to finish
if (tar.waitForFinished(10000) == true)
{
if (tar.exitCode() == 0)
{
statusMessages.append(tar.readAllStandardError());
return true;
}
}
statusMessages.append(tar.readAllStandardError());
statusMessages.append(tar.readAllStandardOutput());
statusMessages.append(QString("Exitcode = %1\n").arg(tar.exitCode()));
return false;
}
It gathers all available process output for you to analyse. Especially look at readAllStandardError().

QtCreator semantic issue warning code will never be executed

I have following chunk of Qt code:
if(this->ueCommunicationsSocket()->bind(QHostAddress(data[0].toString()),
static_cast<quint16>(data[1].toInt())),
QAbstractSocket::ShareAddress)
{
qDebug() << Q_FUNC_INFO
<< "TCP socket bind succesfull";
}
else
{
qDebug() << Q_FUNC_INFO
<< QString::number(this->ueCommunicationsSocket()->error())
<< this->ueCommunicationsSocket()->errorString(); // here i get semantic issue
} // if
and I am getting semantic issue warning code will never be executed. I am aware this is some dumb mistake and I am ready to get downvote(s), however, I cannot find mistake! Here is also a screenshot:
if(this->ueCommunicationsSocket()->bind(QHostAddress(data[0].toString()),
static_cast<quint16>(data[1].toInt())),
QAbstractSocket::ShareAddress)
is
if ( /*something */, QAbstractSocket::ShareAddress)
since AbstractSocket::SharedAddress is 0x1, this condition with a comma operator ! is always true, i.e. the else branch will never be executed.

Qt: cannot open file for writing

I try to access a simple text file from a Qt-widget application with the QFile class for reading an writing. Reading the file line by line as a string works fine. But opening it ready to write fails. The following code checks if the file exists and tries to set the proper permissions, but in the end the file won't open.
Here is the failing piece of code:
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[]){
QApplication app(argc, argv);
MainWindow w;
w.show();
QFile file(":/test.dat");
qDebug() << "exists? " << file.exists();
qDebug() << "writable? " << file.isWritable();
qDebug() << "permissions before? " << file.permissions();
qDebug() << "permissions set? " << file.setPermissions(QFileDevice::WriteOther | QFileDevice::ReadOther);
qDebug() << "permissions after? " << file.permissions();
qDebug() << "opened? " << file.open(QIODevice::Append);
qDebug() << "errors? " << file.errorString();
qDebug() << "errnum? " << file.error();
QTextStream out(&file);
out << "something to append";
file.close();
return app.exec();
}
Qt returns this message:
exists? true
writable? false
permissions before? QFlags(0x4|0x40|0x400|0x4000)
permissions set? false
permissions after? QFlags(0x4|0x40|0x400|0x4000)
opened? false
errors? "Unknown error"
errnum? 5
QIODevice::write (QFile, ":/test.dat"): device not open
If I change the parameter in the open-function to QIODevice::ReadOnly the file is readable without problems, failing with QIODevice::WriteOnly. Why doesn't the same thing work for writing as well? Is it the permission? And why don't the permissions change after I called setPermissions? I run Qt as root on Ubuntu 14.04. And test.dat has full rights -rwxrwxrwx owned by user.
Can someone help?
Thanks!
The author is having Linux-related problem with writing to file created by console process with elevated privileges. I have fully reproduced the problem and when attempted to remove the file with:
vi \home\myuser\Documents\f.txt // create file like that from console
rm \home\myuser\Documents\f.txt // now try to remove it from console
I got "rm: remove write-protected regular file "\home\myuser\Documents\f.txt" and responded "yes" and then the code above shows after creating new file in the context of the program's process:
opened? true
exists? true
writable? true
permissions before? QFlags(0x4|0x20|0x40|0x200|0x400|0x2000|0x4000)
permissions set? true
permissions after? QFlags(0x4|0x20|0x40|0x200|0x400|0x2000|0x4000)
errors? "Unknown error"
errnum? 0
I run Qt Creator as root on Ubuntu 14.04.
It does not ensure the privileges of the program you run from it, I guess. UPDATE: make sure you run the program with appropriate permissions e.g. Root in this case.

Qt - Wait for Qprocess to finish

I'm using CMD by QProcess but I have a problem.
My code:
QProcess process;
process.start("cmd.exe");
process.write ("del f:\\b.txt\n\r");
process.waitForFinished();
process.close();
When I don't pass an argument for waitForFinished() it waits for 30 secs. I want to terminate QProcess after CMD command is executed! Not much and not less!
You need to terminate the cmd.exe by sending exit command, otherwise it will wait for commands
Here is my suggestion:
QProcess process;
process.start("cmd.exe");
process.write ("del f:\\b.txt\n\r");
process.write ("exit\n\r");
process.waitForFinished();
process.close();
The process you're starting is cmd.exe which, by itself will, not terminate. If you call cmd with arguments, you should achieve what you want: -
QProcess process;
process.start("cmd.exe \"del f:\\b.txt"\"");
process.waitForFinished();
process.close();
Note that the arguments are escaped in quotes.
Alternatively, you could call the del process, without cmd: -
QProcess process;
process.start("del \"f:\\b.txt"\"");
process.waitForFinished();
process.close();
Finally, if you just want to delete a file, you could use the QFile::remove function.
QFile file("f:\\b.txt");
if(file.remove())
qDebug() << "File removed successfully";

Qt can't kill process

In my Qt app i run a process under a push-button click.The process run gnome-terminal.My problem is when i kill qt process that run but push-button click from another button by pid,its shows error."kill: sending signal to 19771 failed: No such process" but still terminal running.And if i kill my app,but still terminal running.
QProcess *p = new QProcess(this);
if (p)
{
p->setEnvironment( QProcess::systemEnvironment() );
p->setProcessChannelMode( QProcess::MergedChannels );
QString program = "gnome-terminal";
QStringList arguments;
arguments << "-x" << "bash" << "--rcfile" << "./auto.sh";
p->start(program, arguments);
pid= p->pid();
}
Button2 cod is:
QProcess::startDetached("kill -9 "+QString(pid));
how can kill process and also terminal by click another push-button?

Resources