Em meu projeto possuo um Main
este responsável por executar uma QThread
e neste roda um QProcess
responsável por criar um arquivo de backup do postgreSQL.
Pois bem o main funciona corretamente chamando a QThread
que também funciona criando e executando QProcess
, assim criando o arquivo.
Porém eu necessito saber quando o processo termina, então criei um connect com um signal já existente no QProces
" de nome finished, este que tem como SLOT criado por mim, porém este signal nunca é chamado gostaria de saber se há algo errado em meu(que já revisei várias vezes), ou se não é possível utilizar este signal em um QThread
.
Qualquer dica é bem vinda. Obrigado.
Segue o código:
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";
}
//-----------------------------------------------------------------------------