PDA

View Full Version : Problem to implement a QThread



guidupas
18th February 2014, 22:09
Hello!

I am having a problem to implement a QThread. My application crash when the thread is started - processo->start()

Help me please. Thanks a lot.

Code below:


//defconexao.cpp
#include "defconexao.h"
#include "ui_defconexao.h"
#include "limpalayout.h"
#include "caixadialogos.h"
#include "verificaos.h"
#include "caixamensagens.h"
#include "criaconfservidor.h"

#include <QCoreApplication>
#include <QProgressBar>

#include <QDebug>

defConexao::defConexao(QWidget *parent) :
QDialog(parent),
ui(new Ui::defConexao)
{
ui->setupUi(this);

verticalLayout_3 = new QVBoxLayout;
limparLayout = new limpaLayout;
this->ui->verticalLayout->addLayout(verticalLayout_3);
}

defConexao::~defConexao()
{
delete ui;
}

void defConexao::on_cancelarButton_clicked()
{
this->close();
}

void defConexao::on_tipoConexaoComboBox_currentIndexCha nged(int index)
{
if(index == 0)
{
limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);
ui->okButton->setEnabled(false);
}
else
{
if(index == 1)
{
limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);

horizontalLayout_2 = new QHBoxLayout;

avisoConfigExisteLabel = new QLabel("<font color=\"red\">Utilize esta opção apenas se o arquivo de configuração<br/>já existir");
avisoConfigExisteLabel->setAlignment(Qt::AlignHCenter);

caminhoArquivoLineEdit = new QLineEdit();
caminhoArquivoLineEdit->setReadOnly(true);
selecionaArquivoButton = new QPushButton("Selecione o arquivo", this);

this->horizontalLayout_2->addWidget(caminhoArquivoLineEdit);
this->horizontalLayout_2->addWidget(selecionaArquivoButton);

this->verticalLayout_3->addWidget(avisoConfigExisteLabel);
this->verticalLayout_3->addLayout(horizontalLayout_2);

barraProgresso = new QProgressBar;
this->verticalLayout_3->addWidget(barraProgresso);

ui->okButton->setEnabled(true);

connect(selecionaArquivoButton, SIGNAL(clicked()), this, SLOT(on_selecionaArquivoButton_clicked()));
}
else
{
if(index == 2)
{
limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);

codigoIdBdLabel = new QLabel("Código de identificação do banco de dados");
codigoIdBdLineEdit = new QLineEdit();

this->verticalLayout_3->addWidget(codigoIdBdLabel);
this->verticalLayout_3->addWidget(codigoIdBdLineEdit);

barraProgresso = new QProgressBar;
this->verticalLayout_3->addWidget(barraProgresso);

ui->okButton->setEnabled(true);
}
else
{
if(index == 3)
{
limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);

horizontalLayout_2 = new QHBoxLayout;

verticalLayout_4 = new QVBoxLayout;
verticalLayout_5 = new QVBoxLayout;

enderecodBdLabel = new QLabel("IP ou HOST do banco de dados");
enderecoBdLineEdit = new QLineEdit();

portaBdLabel = new QLabel("Porta");
portaBdLineEdit = new QLineEdit();
portaBdLineEdit->setText("5432");
portaBdLineEdit->setMaximumWidth(70);
portaBdLineEdit->setValidator(new QIntValidator(this));

this->verticalLayout_4->addWidget(enderecodBdLabel);
this->verticalLayout_4->addWidget(enderecoBdLineEdit);

this->verticalLayout_5->addWidget(portaBdLabel);
this->verticalLayout_5->addWidget(portaBdLineEdit);

this->horizontalLayout_2->addLayout(verticalLayout_4);
this->horizontalLayout_2->addLayout(verticalLayout_5);

this->verticalLayout_3->addLayout(horizontalLayout_2);

barraProgresso = new QProgressBar;
this->verticalLayout_3->addWidget(barraProgresso);

ui->okButton->setEnabled(true);
}
}
}
}
}

void defConexao::on_selecionaArquivoButton_clicked()
{
//this->caminhoArquivoLineEdit->setText(caixaDeDialogos->abrirArquivo("Arquivo de configuração do banco de dados","Todos os arquivos (*.*);;Arquivo XML (*.xml)"));
this->caminhoArquivoLineEdit->setText(caixaDeDialogos->abrirArquivo("Arquivo de configuração do banco de dados","Arquivo XML (*.xml)"));
}

void defConexao::on_okButton_clicked()
{
if(ui->tipoConexaoComboBox->currentIndex() == 1)
{
if(this->caminhoArquivoLineEdit->text() == "")
{
this->caminhoArquivoLineEdit->setStyleSheet("border: 1px solid red");
}
else
{
this->caminhoArquivoLineEdit->setStyleSheet("");

}
}
else
{
if(ui->tipoConexaoComboBox->currentIndex() == 2)
{
if(this->codigoIdBdLineEdit->text() == "")
{
this->codigoIdBdLineEdit->setStyleSheet("border: 1px solid red");
}
else
{
this->codigoIdBdLineEdit->setStyleSheet("");

}
}
else
{
bool camposCompletos = true;
if(ui->tipoConexaoComboBox->currentIndex() == 3)
{
if(this->enderecoBdLineEdit->text() == "")
{
camposCompletos = false;
this->enderecoBdLineEdit->setStyleSheet("border: 1px solid red");
}
else
{
this->enderecoBdLineEdit->setStyleSheet("");
}

if(this->portaBdLineEdit->text() == "")
{
camposCompletos = false;
this->portaBdLineEdit->setStyleSheet("border: 1px solid red");
}
else
{
this->portaBdLineEdit->setStyleSheet("");
}

if(camposCompletos == true)
{
QThread processo;
criaConfServidor *criaConf = new criaConfServidor;
criaConf->connectaThreadComSlot(processo);
criaConf->moveToThread(&processo);
//processo.start();
}
}
}
}
}



//defconexao.h
#ifndef DEFCONEXAO_H
#define DEFCONEXAO_H

#include "limpalayout.h"
#include "caixadialogos.h"
#include "criaconfservidor.h"

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QLayout>
#include <QProgressBar>

namespace Ui {
class defConexao;
}

class defConexao : public QDialog
{
Q_OBJECT

public:
explicit defConexao(QWidget *parent = 0);
~defConexao();

bool verificaArquivoExiste();
//void criaArquivoConfCloud(QString codigo);
//void criaArquivoConfServidor();
//void verificaArquivoEstrutura(QString caminho);

private slots:
void on_cancelarButton_clicked();
void on_tipoConexaoComboBox_currentIndexChanged(int index);
void on_selecionaArquivoButton_clicked();
void on_okButton_clicked();

private:
Ui::defConexao *ui;

QLabel *codigoIdBdLabel;
QLineEdit *codigoIdBdLineEdit;

QLabel *enderecodBdLabel;
QLineEdit *enderecoBdLineEdit;
QLabel *portaBdLabel;
QLineEdit *portaBdLineEdit;

QLabel *avisoConfigExisteLabel;
QLineEdit *caminhoArquivoLineEdit;
QPushButton *selecionaArquivoButton;

QVBoxLayout *verticalLayout_3;
QVBoxLayout *verticalLayout_4;
QVBoxLayout *verticalLayout_5;
QHBoxLayout *horizontalLayout_2;

QProgressBar *barraProgresso;

limpaLayout *limparLayout;

caixaDialogos *caixaDeDialogos;
};

#endif // DEFCONEXAO_H



//criaconfservidor.cpp
#include "criaconfservidor.h"
//#include "bancodados.h"

//#include <QSql>
//#include <QSqlDatabase>
//#include <QDebug>

criaConfServidor::criaConfServidor(QObject *parent) :
QObject(parent)
{
}

void criaConfServidor::connectaThreadComSlot(QThread &processo)
{
qDebug() << "Conexão efetuada";
connect(&processo, SIGNAL(started()), this, SLOT(criaArquivoConf()));
}

void criaConfServidor::criaArquivoConf()
{
qDebug() << "Entrou";
}



//criaconfservidor.h
#ifndef CRIACONFSERVIDOR_H
#define CRIACONFSERVIDOR_H

#include <QObject>
#include <QThread>
#include <QDebug>

class criaConfServidor : public QObject
{
Q_OBJECT
public:
//explicit criaConfServidor(QObject *parent = 0, QString DBHost = "192.168.0.1", QString DBPorta = "5432");
explicit criaConfServidor(QObject *parent = 0);
void connectaThreadComSlot(QThread &processo);

signals:

public slots:
void criaArquivoConf();

};

#endif // CRIACONFSERVIDOR_H

anda_skoa
18th February 2014, 22:35
You destroy the thread object after starting it, i.e. the variable "processo" goes out of scope.

Cheers,
_

sulliwk06
18th February 2014, 22:35
If your program is crashing at "//processo.start();", I'd say it's because your QThread processo; is local and goes out of scope. Try making it global.

guidupas
18th February 2014, 22:42
So, where it must be declared?

ChrisW67
19th February 2014, 00:32
Somewhere that gives it a lifetime longer than the function. Either a member variable of the class or allocate the QProcess object on the heap remembering to arrange deletion at the appropriate time.

guidupas
20th February 2014, 18:36
It worked. Thank you very much people. I created the Qthread in .h file and iniciated it in .cpp file.