How to check for 30sec if file in folder exist? - qt

I want to check if in a Folder are a json-File called "test.json" for 30 sec.
I have in Qt a Button, which execute a python skript --> this skript create the test.json.
When I clicked the Button now i get the file successfully.
How can I checked if a file is in a Folder for 30 sec?
Any Idea?

you can use a QTimer
with singleshot = true and interval to 30 000
t = new QTimer(this);
t->setSingleShot(true);
t->setInterval(30 * 1000);
connect(t, &QTimer::timeout, [](){
qDebug() << "is the file already there: " << QFile::exists("/home/pw/file.txt");});
t->start();

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().

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

QFile::copy says "can't open source file for input"?

I'm going to copy one file using QFile::copy function but this function always returns false and errorString says :
"Cannot open D:/tmp/buf34.txt for input"
I tried to run this program with administrator privilege but nothing changed. My code is really simple :
QString source = url.toLocalFile();
QString destination = _dir.absolutePath()
+ QString("/%1").arg(QFileInfo(source).fileName());
qDebug()<<"Cp from :" << source << " to : "<< destination;
QFile file(source);
qDebug()<<file.copy(destination);
qDebug()<<file.errorString();
Edit:
I have QListView occupied with a QFileSystemModel. I try to drag one file from this ListView to a QLabel. For the QLabel a destination path is set. In drop event I try to copy file.
QFile::copy uses QFile::open but overwrites the error message open would give by the unhelpful "Cannot open %1 for input" you got.
So, you should try opening the file yourself to get that original error message:
qDebug()<<file.open(QFile::ReadOnly);
qDebug()<<file.errorString();

QProcess and shell : Destroyed while process is still running

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.

Resources