[SOLVED] Problem to implement a QThread



  • 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

    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_currentIndexChanged(int index)
    {
    if(index == 0)
    {
    limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);
    ui->okButton->setEnabled(false);
    }
    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_okButton_clicked()
    {
    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

    namespace Ui {
    class defConexao;
    }
    class defConexao : public QDialog
    {
    Q_OBJECT

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

    bool verificaArquivoExiste();
    

    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

    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

    class criaConfServidor : public QObject
    {
    Q_OBJECT
    public:
    explicit criaConfServidor(QObject *parent = 0);
    void connectaThreadComSlot(QThread &processo);

    signals:

    public slots:
    void criaArquivoConf();

    };

    #endif // CRIACONFSERVIDOR_H
    @



  • You say you have a problem with processo->start(), but the actual code has it as a local variable. Once it leaves scope, the thread will be destoryed. The documentation says this will cause a crash when deleting a thread when isFinished returns false.



  • So, where it must be declared?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.