Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problem to implement a QThread
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem to implement a QThread

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • guidupasG Offline
    guidupasG Offline
    guidupas
    wrote on last edited by
    #1

    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
    @

    Att.
    Guilherme Cortada Dupas

    1 Reply Last reply
    0
    • S Offline
      S Offline
      steno
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • guidupasG Offline
        guidupasG Offline
        guidupas
        wrote on last edited by
        #3

        So, where it must be declared?

        Att.
        Guilherme Cortada Dupas

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved