QProcess not working.
-
I've changed the code back to:
process* pobjProcess(new process(this)); if ( pobjProcess != nullptr ) { QString strApp(DataSets::mscszMariaDBdumpApp); QStringList slstArguments; slstArguments << ("\"" + strDBinstFolder + strApp + "\"") << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase()) << QString(">") << strFilename; qdbg() << slstArguments; qdbg() << strApp; pobjProcess->setWorkingDirectory(strDBinstFolder); pobjProcess->setArguments(slstArguments); pobjProcess->setProgram(strApp); pobjProcess->start(); QObject::connect(pobjProcess, &QProcess::errorOccurred, [this](QProcess::ProcessError err) { qdbg() << "Error: " << err; } ); QObject::connect(pobjProcess, &QProcess::stateChanged, [pobjProcess](QProcess::ProcessState newState) { const qint64 cint64PID(pobjProcess->processId()); if ( newState == QProcess::NotRunning ) { } else if ( newState == QProcess::Running && cint64PID > 0 ) { } qdbg() << "PID: " << cint64PID << ", newState: " << newState; } ); }
Still doesn't work and I'm not seeing anything errorOccurred
-
@J-Hilk , yes, it didn't originally, I add those later just to see if it made any difference.
@SPlatten said in QProcess not working.:
yes, it didn't originally, I add those later just to see if it made any difference.
I don't really know what is the purpose of this, I guess you have to get SQL statements from you DB.
My way to achieve it would be:
- start
mysqldump
withQProcess
- wait until
QProcess
/mysqldump
finished - read
QProcess
output (cf.QProcess::readAll()
) ==> no need of redirection at all
And last remark:
you have to do connection before starting process, to be sure not to loose state changes ;) - start
-
@SPlatten said in QProcess not working.:
yes, it didn't originally, I add those later just to see if it made any difference.
I don't really know what is the purpose of this, I guess you have to get SQL statements from you DB.
My way to achieve it would be:
- start
mysqldump
withQProcess
- wait until
QProcess
/mysqldump
finished - read
QProcess
output (cf.QProcess::readAll()
) ==> no need of redirection at all
And last remark:
you have to do connection before starting process, to be sure not to loose state changes ;)@KroMignon I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.
- start
-
@SPlatten said in QProcess not working.:
@JonB , where are you getting process and exec from?
It's an example of
QProcess::exec()
. Actually that'sstatic
. Exactly the same principle if you want to usepobjProcess->start();
. It's the argument passing I'm trying to tell you about.if I copy the command line and arguments to a command prompt it works,
Yes, as I said this will (only) work from
cmd
.<< QString(">") << strFilename;
As I said, this won't work for
mysqldump
run as an executable. It will only work viacmd /c ...
. Up to you whether you believe me :) -
@KroMignon I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.
@SPlatten said in QProcess not working.:
I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.
With QProcess, you don't start the process from a shell / command prompt, that is the difference here.
Try this:auto proc = new QProcess; QObject::connect(proc, &QProcess::stateChanged, [proc](QProcess::ProcessState newState) { qDebug() << "new state is " << newState; if(newState == QProcess::NotRunning) { QString output(proc->readAll()); qDebug() << "Output is" << output; proc->delateLater(); } }); proc->setWorkingDirectory(strDBinstFolder); proc->start(strApp,slstArguments );
EDIT: do NOT add redirection in slstArguments !!!
EDIT BIS:
QStringList slstArguments; slstArguments << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase());
==> I suppose that DataSets::mscszOptionHost contains
--host=
-
@SPlatten said in QProcess not working.:
@JonB , where are you getting process and exec from?
It's an example of
QProcess::exec()
. Actually that'sstatic
. Exactly the same principle if you want to usepobjProcess->start();
. It's the argument passing I'm trying to tell you about.if I copy the command line and arguments to a command prompt it works,
Yes, as I said this will (only) work from
cmd
.<< QString(">") << strFilename;
As I said, this won't work for
mysqldump
run as an executable. It will only work viacmd /c ...
. Up to you whether you believe me :) -
@SPlatten
int QProcess::execute(const QString &program, const QStringList &arguments) [static]. So it'sexecute
. It was introduced into some Qt. I don't know about 5.9.2.As I said, forget I ever wrote
exec
orexecute
. The same argument processing applies toQProcess::start
. You keep passing>
argument, and I've explained this won't work. -
@SPlatten said in QProcess not working.:
I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.
With QProcess, you don't start the process from a shell / command prompt, that is the difference here.
Try this:auto proc = new QProcess; QObject::connect(proc, &QProcess::stateChanged, [proc](QProcess::ProcessState newState) { qDebug() << "new state is " << newState; if(newState == QProcess::NotRunning) { QString output(proc->readAll()); qDebug() << "Output is" << output; proc->delateLater(); } }); proc->setWorkingDirectory(strDBinstFolder); proc->start(strApp,slstArguments );
EDIT: do NOT add redirection in slstArguments !!!
EDIT BIS:
QStringList slstArguments; slstArguments << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase());
==> I suppose that DataSets::mscszOptionHost contains
--host=
@KroMignon , I've tried this:
QString strApp(DataSets::mscszMariaDBdumpApp); QStringList slstArguments; slstArguments << ("\"" + strDBinstFolder + strApp + "\"") << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase()) << QString(">") << strFilename; pobjProcess->setWorkingDirectory(strDBinstFolder); pobjProcess->start(strApp, slstArguments); QObject::connect(pobjProcess, &QProcess::errorOccurred, [this](QProcess::ProcessError err) { qDebug() << "Error: " << err; }); QObject::connect(pobjProcess, &QProcess::stateChanged, [pobjProcess](QProcess::ProcessState newState) { const qint64 cint64PID(pobjProcess->processId()); if ( newState == QProcess::NotRunning ) { } else if ( newState == QProcess::Running && cint64PID > 0 ) { } qDebug() << "PID: " << cint64PID << ", newState: " << newState; });
-
@KroMignon , I've tried this:
QString strApp(DataSets::mscszMariaDBdumpApp); QStringList slstArguments; slstArguments << ("\"" + strDBinstFolder + strApp + "\"") << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase()) << QString(">") << strFilename; pobjProcess->setWorkingDirectory(strDBinstFolder); pobjProcess->start(strApp, slstArguments); QObject::connect(pobjProcess, &QProcess::errorOccurred, [this](QProcess::ProcessError err) { qDebug() << "Error: " << err; }); QObject::connect(pobjProcess, &QProcess::stateChanged, [pobjProcess](QProcess::ProcessState newState) { const qint64 cint64PID(pobjProcess->processId()); if ( newState == QProcess::NotRunning ) { } else if ( newState == QProcess::Running && cint64PID > 0 ) { } qDebug() << "PID: " << cint64PID << ", newState: " << newState; });
@SPlatten Did you notice "EDIT: do NOT add redirection in slstArguments !!!" in @KroMignon post?
Why do you need redirection to a file if you can simply read the stdout from the process as @KroMignon already suggested? -
@KroMignon , I've tried this:
QString strApp(DataSets::mscszMariaDBdumpApp); QStringList slstArguments; slstArguments << ("\"" + strDBinstFolder + strApp + "\"") << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase()) << QString(">") << strFilename; pobjProcess->setWorkingDirectory(strDBinstFolder); pobjProcess->start(strApp, slstArguments); QObject::connect(pobjProcess, &QProcess::errorOccurred, [this](QProcess::ProcessError err) { qDebug() << "Error: " << err; }); QObject::connect(pobjProcess, &QProcess::stateChanged, [pobjProcess](QProcess::ProcessState newState) { const qint64 cint64PID(pobjProcess->processId()); if ( newState == QProcess::NotRunning ) { } else if ( newState == QProcess::Running && cint64PID > 0 ) { } qDebug() << "PID: " << cint64PID << ", newState: " << newState; });
-
@SPlatten Did you notice "EDIT: do NOT add redirection in slstArguments !!!" in @KroMignon post?
Why do you need redirection to a file if you can simply read the stdout from the process as @KroMignon already suggested? -
@jsulm The purpose of this is to provide an export / backup facility of the database to an SQL file.
-
@SPlatten This don't make sense to me:
- Why do you add application into argument list?
- Why to you redirect it to a file and not directly read it for QProcess stream?
@KroMignon , as I said earlier I've tried numerous things, thats just one of variant.
-
@KroMignon , as I said earlier I've tried numerous things, thats just one of variant.
-
@SPlatten
int QProcess::execute(const QString &program, const QStringList &arguments) [static]. So it'sexecute
. It was introduced into some Qt. I don't know about 5.9.2.As I said, forget I ever wrote
exec
orexecute
. The same argument processing applies toQProcess::start
. You keep passing>
argument, and I've explained this won't work. -
@SPlatten said in QProcess not working.:
I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.
With QProcess, you don't start the process from a shell / command prompt, that is the difference here.
Try this:auto proc = new QProcess; QObject::connect(proc, &QProcess::stateChanged, [proc](QProcess::ProcessState newState) { qDebug() << "new state is " << newState; if(newState == QProcess::NotRunning) { QString output(proc->readAll()); qDebug() << "Output is" << output; proc->delateLater(); } }); proc->setWorkingDirectory(strDBinstFolder); proc->start(strApp,slstArguments );
EDIT: do NOT add redirection in slstArguments !!!
EDIT BIS:
QStringList slstArguments; slstArguments << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << (Trainer::strDatabase());
==> I suppose that DataSets::mscszOptionHost contains
--host=
@KroMignon , yes it does, I'll try without the application which is where I started.
-
@JonB , just tried:
QProcess::execute(strApp);
strApp contains:
/c "C:/Program Files (x86)/MariaDB 10.5/bin/mysqldump" --host=localhost --user=trainer --password=CobhamAC training > C:/Users/simon.platten/Documents/TrainerS1D5.sql
Still not working.
@SPlatten said in QProcess not working.:
QProcess::execute(strApp);
First parameter is the executable ("cmd.exe" in your case), second is a list of parameters...
-
@JonB , just tried:
QProcess::execute(strApp);
strApp contains:
/c "C:/Program Files (x86)/MariaDB 10.5/bin/mysqldump" --host=localhost --user=trainer --password=CobhamAC training > C:/Users/simon.platten/Documents/TrainerS1D5.sql
Still not working.
@SPlatten
No, that is not what I said at all. Look at the example. The program needs to becmd.exe
. The arguments need to be first/c
and second your whole command line (as a single argument; forcmd
it doesn't seem to matter if you pass it as one string as I showed, or I think it will accept each argument separately). I showed you earlier an example of what you need. -
@SPlatten
No, that is not what I said at all. Look at the example. The program needs to becmd.exe
. The arguments need to be first/c
and second your whole command line (as a single argument; forcmd
it doesn't seem to matter if you pass it as one string as I showed, or I think it will accept each argument separately). I showed you earlier an example of what you need. -
@SPlatten
Having said that. If the only purpose of having to go viacmd /c
is to handle the redirection of output via>
, I would handle that back inQProcess
(I think others have suggested this) and then you won't need to go viacmd
, which has its disadvantages.