In my project I have a Main
responsible for executing a QThread
and in this it runs QProcess
responsible for creating a postgreSQL backup file.
Well, the main works correctly by calling QThread
which also works by creating and executing QProcess
, thus creating the file.
But I need to know when the process terminates, so I created a connect with an signal already existing in the% , this one has as SLOT created by me, but this signal is never called I would like to know if there is something wrong in my (which I have reviewed several times) you can use this signal in a QProces
.
Any tips are welcome. Thank you.
Follow the code:
ThreadPgDump.h:
#ifndef THREADPGDUMP_H
#define THREADPGDUMP_H
#include "QObject"
#include "QThread"
#include "QString"
#include "QProcess
class ThreadPgDump : public QThread{
Q_OBJECT
public:
explicit ThreadPgDump(QObject *parent = 0);
~ThreadPgDump();
//---------------------------------
// SET
void setPgDumpPath(QString pgDumpPath) { this->hostName = pgDumpPath; }
void setParameters(
QString hostName,
QString databaseName,
int port,
QString username,
QString password,
QString backupPathFileName)
{
this->hostName = hostName;
this->databaseName = databaseName;
this->port = port;
this->username = username;
this->password = password;
this->backupPathFileName = backupPathFileName;
}
void kill();
private:
QString pgDumpPath;
QString hostName;
QString databaseName;
int port;
QString username;
QString password;
QString backupPathFileName;
QProcess *process;
//---------------------------------
// FUNC
bool procDefineEnvironment();
QString getPgDump();
protected:
void run();
signals:
void finishedPgDump();
public slots:
void slotProgressFinished(int);
};
#endif // THREADPGDUMP_H
ThreadPgDump.cpp:
#include "ThreadPgDump.h"
#include "qdebug.h"
#include "QProcess"
//-----------------------------------------------------------------------------
ThreadPgDump::ThreadPgDump(QObject *parent) : QThread(parent){
}
//-----------------------------------------------------------------------------
ThreadPgDump::~ThreadPgDump(){
}
//-----------------------------------------------------------------------------
void ThreadPgDump::run(){
process = new QProcess(this->parent());
connect(process, SIGNAL(finished(int)), this, SLOT(slotProgressFinished(int)));
procDefineEnvironment();
QString command;
command.clear();
command.append(getPgDump());
command.append("--host ").append(hostName).append(" ");
command.append("--port ").append(QString().sprintf("%d", port)).append(" ");
command.append("--username \"").append(username).append("\" ");
command.append("--format custom ");
command.append("--blobs ");
command.append("--verbose ");
command.append("--file \"").append(backupPathFileName).append("\" ");
command.append("\"").append(databaseName).append("\"");
process->start(command);
}
//-----------------------------------------------------------------------------
QString ThreadPgDump::getPgDump(){
if( pgDumpPath.length() > 0 ){
return pgDumpPath;
}
else{
#ifdef __linux__
return QString("/usr/bin/pg_dump ");
#endif
#ifdef WIN32
return QString("c:/pg_dump.exe ");
#endif
}
}
//-----------------------------------------------------------------------------
void ThreadPgDump::kill(){
if( process != NULL ){
process->kill();
}
}
//-----------------------------------------------------------------------------
bool ThreadPgDump::procDefineEnvironment(){
QStringList env = QProcess::systemEnvironment();
env.append(QString("PGPASSWORD=").append(password));
process->setEnvironment(env);
process->setProcessChannelMode(QProcess::MergedChannels);
return true;
}
//-----------------------------------------------------------------------------
void ThreadPgDump::slotProgressFinished(int exitNumber){
qDebug() << "TESTE";
}
//-----------------------------------------------------------------------------