Results 1 to 6 of 6

Thread: Problem to implement a QThread

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:
    Qt Code:
    1. //defconexao.cpp
    2. #include "defconexao.h"
    3. #include "ui_defconexao.h"
    4. #include "limpalayout.h"
    5. #include "caixadialogos.h"
    6. #include "verificaos.h"
    7. #include "caixamensagens.h"
    8. #include "criaconfservidor.h"
    9.  
    10. #include <QCoreApplication>
    11. #include <QProgressBar>
    12.  
    13. #include <QDebug>
    14.  
    15. defConexao::defConexao(QWidget *parent) :
    16. QDialog(parent),
    17. ui(new Ui::defConexao)
    18. {
    19. ui->setupUi(this);
    20.  
    21. verticalLayout_3 = new QVBoxLayout;
    22. limparLayout = new limpaLayout;
    23. this->ui->verticalLayout->addLayout(verticalLayout_3);
    24. }
    25.  
    26. defConexao::~defConexao()
    27. {
    28. delete ui;
    29. }
    30.  
    31. void defConexao::on_cancelarButton_clicked()
    32. {
    33. this->close();
    34. }
    35.  
    36. void defConexao::on_tipoConexaoComboBox_currentIndexChanged(int index)
    37. {
    38. if(index == 0)
    39. {
    40. limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);
    41. ui->okButton->setEnabled(false);
    42. }
    43. else
    44. {
    45. if(index == 1)
    46. {
    47. limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);
    48.  
    49. horizontalLayout_2 = new QHBoxLayout;
    50.  
    51. avisoConfigExisteLabel = new QLabel("<font color=\"red\">Utilize esta opção apenas se o arquivo de configuração<br/>já existir");
    52. avisoConfigExisteLabel->setAlignment(Qt::AlignHCenter);
    53.  
    54. caminhoArquivoLineEdit = new QLineEdit();
    55. caminhoArquivoLineEdit->setReadOnly(true);
    56. selecionaArquivoButton = new QPushButton("Selecione o arquivo", this);
    57.  
    58. this->horizontalLayout_2->addWidget(caminhoArquivoLineEdit);
    59. this->horizontalLayout_2->addWidget(selecionaArquivoButton);
    60.  
    61. this->verticalLayout_3->addWidget(avisoConfigExisteLabel);
    62. this->verticalLayout_3->addLayout(horizontalLayout_2);
    63.  
    64. barraProgresso = new QProgressBar;
    65. this->verticalLayout_3->addWidget(barraProgresso);
    66.  
    67. ui->okButton->setEnabled(true);
    68.  
    69. connect(selecionaArquivoButton, SIGNAL(clicked()), this, SLOT(on_selecionaArquivoButton_clicked()));
    70. }
    71. else
    72. {
    73. if(index == 2)
    74. {
    75. limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);
    76.  
    77. codigoIdBdLabel = new QLabel("Código de identificação do banco de dados");
    78. codigoIdBdLineEdit = new QLineEdit();
    79.  
    80. this->verticalLayout_3->addWidget(codigoIdBdLabel);
    81. this->verticalLayout_3->addWidget(codigoIdBdLineEdit);
    82.  
    83. barraProgresso = new QProgressBar;
    84. this->verticalLayout_3->addWidget(barraProgresso);
    85.  
    86. ui->okButton->setEnabled(true);
    87. }
    88. else
    89. {
    90. if(index == 3)
    91. {
    92. limparLayout->limpaWidgetLayout(this->verticalLayout_3, false);
    93.  
    94. horizontalLayout_2 = new QHBoxLayout;
    95.  
    96. verticalLayout_4 = new QVBoxLayout;
    97. verticalLayout_5 = new QVBoxLayout;
    98.  
    99. enderecodBdLabel = new QLabel("IP ou HOST do banco de dados");
    100. enderecoBdLineEdit = new QLineEdit();
    101.  
    102. portaBdLabel = new QLabel("Porta");
    103. portaBdLineEdit = new QLineEdit();
    104. portaBdLineEdit->setText("5432");
    105. portaBdLineEdit->setMaximumWidth(70);
    106. portaBdLineEdit->setValidator(new QIntValidator(this));
    107.  
    108. this->verticalLayout_4->addWidget(enderecodBdLabel);
    109. this->verticalLayout_4->addWidget(enderecoBdLineEdit);
    110.  
    111. this->verticalLayout_5->addWidget(portaBdLabel);
    112. this->verticalLayout_5->addWidget(portaBdLineEdit);
    113.  
    114. this->horizontalLayout_2->addLayout(verticalLayout_4);
    115. this->horizontalLayout_2->addLayout(verticalLayout_5);
    116.  
    117. this->verticalLayout_3->addLayout(horizontalLayout_2);
    118.  
    119. barraProgresso = new QProgressBar;
    120. this->verticalLayout_3->addWidget(barraProgresso);
    121.  
    122. ui->okButton->setEnabled(true);
    123. }
    124. }
    125. }
    126. }
    127. }
    128.  
    129. void defConexao::on_selecionaArquivoButton_clicked()
    130. {
    131. //this->caminhoArquivoLineEdit->setText(caixaDeDialogos->abrirArquivo("Arquivo de configuração do banco de dados","Todos os arquivos (*.*);;Arquivo XML (*.xml)"));
    132. this->caminhoArquivoLineEdit->setText(caixaDeDialogos->abrirArquivo("Arquivo de configuração do banco de dados","Arquivo XML (*.xml)"));
    133. }
    134.  
    135. void defConexao::on_okButton_clicked()
    136. {
    137. if(ui->tipoConexaoComboBox->currentIndex() == 1)
    138. {
    139. if(this->caminhoArquivoLineEdit->text() == "")
    140. {
    141. this->caminhoArquivoLineEdit->setStyleSheet("border: 1px solid red");
    142. }
    143. else
    144. {
    145. this->caminhoArquivoLineEdit->setStyleSheet("");
    146.  
    147. }
    148. }
    149. else
    150. {
    151. if(ui->tipoConexaoComboBox->currentIndex() == 2)
    152. {
    153. if(this->codigoIdBdLineEdit->text() == "")
    154. {
    155. this->codigoIdBdLineEdit->setStyleSheet("border: 1px solid red");
    156. }
    157. else
    158. {
    159. this->codigoIdBdLineEdit->setStyleSheet("");
    160.  
    161. }
    162. }
    163. else
    164. {
    165. bool camposCompletos = true;
    166. if(ui->tipoConexaoComboBox->currentIndex() == 3)
    167. {
    168. if(this->enderecoBdLineEdit->text() == "")
    169. {
    170. camposCompletos = false;
    171. this->enderecoBdLineEdit->setStyleSheet("border: 1px solid red");
    172. }
    173. else
    174. {
    175. this->enderecoBdLineEdit->setStyleSheet("");
    176. }
    177.  
    178. if(this->portaBdLineEdit->text() == "")
    179. {
    180. camposCompletos = false;
    181. this->portaBdLineEdit->setStyleSheet("border: 1px solid red");
    182. }
    183. else
    184. {
    185. this->portaBdLineEdit->setStyleSheet("");
    186. }
    187.  
    188. if(camposCompletos == true)
    189. {
    190. QThread processo;
    191. criaConfServidor *criaConf = new criaConfServidor;
    192. criaConf->connectaThreadComSlot(processo);
    193. criaConf->moveToThread(&processo);
    194. //processo.start();
    195. }
    196. }
    197. }
    198. }
    199. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //defconexao.h
    2. #ifndef DEFCONEXAO_H
    3. #define DEFCONEXAO_H
    4.  
    5. #include "limpalayout.h"
    6. #include "caixadialogos.h"
    7. #include "criaconfservidor.h"
    8.  
    9. #include <QDialog>
    10. #include <QLabel>
    11. #include <QLineEdit>
    12. #include <QLayout>
    13. #include <QProgressBar>
    14.  
    15. namespace Ui {
    16. class defConexao;
    17. }
    18.  
    19. class defConexao : public QDialog
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit defConexao(QWidget *parent = 0);
    25. ~defConexao();
    26.  
    27. bool verificaArquivoExiste();
    28. //void criaArquivoConfCloud(QString codigo);
    29. //void criaArquivoConfServidor();
    30. //void verificaArquivoEstrutura(QString caminho);
    31.  
    32. private slots:
    33. void on_cancelarButton_clicked();
    34. void on_tipoConexaoComboBox_currentIndexChanged(int index);
    35. void on_selecionaArquivoButton_clicked();
    36. void on_okButton_clicked();
    37.  
    38. private:
    39. Ui::defConexao *ui;
    40.  
    41. QLabel *codigoIdBdLabel;
    42. QLineEdit *codigoIdBdLineEdit;
    43.  
    44. QLabel *enderecodBdLabel;
    45. QLineEdit *enderecoBdLineEdit;
    46. QLabel *portaBdLabel;
    47. QLineEdit *portaBdLineEdit;
    48.  
    49. QLabel *avisoConfigExisteLabel;
    50. QLineEdit *caminhoArquivoLineEdit;
    51. QPushButton *selecionaArquivoButton;
    52.  
    53. QVBoxLayout *verticalLayout_3;
    54. QVBoxLayout *verticalLayout_4;
    55. QVBoxLayout *verticalLayout_5;
    56. QHBoxLayout *horizontalLayout_2;
    57.  
    58. QProgressBar *barraProgresso;
    59.  
    60. limpaLayout *limparLayout;
    61.  
    62. caixaDialogos *caixaDeDialogos;
    63. };
    64.  
    65. #endif // DEFCONEXAO_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //criaconfservidor.cpp
    2. #include "criaconfservidor.h"
    3. //#include "bancodados.h"
    4.  
    5. //#include <QSql>
    6. //#include <QSqlDatabase>
    7. //#include <QDebug>
    8.  
    9. criaConfServidor::criaConfServidor(QObject *parent) :
    10. QObject(parent)
    11. {
    12. }
    13.  
    14. void criaConfServidor::connectaThreadComSlot(QThread &processo)
    15. {
    16. qDebug() << "Conexão efetuada";
    17. connect(&processo, SIGNAL(started()), this, SLOT(criaArquivoConf()));
    18. }
    19.  
    20. void criaConfServidor::criaArquivoConf()
    21. {
    22. qDebug() << "Entrou";
    23. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //criaconfservidor.h
    2. #ifndef CRIACONFSERVIDOR_H
    3. #define CRIACONFSERVIDOR_H
    4.  
    5. #include <QObject>
    6. #include <QThread>
    7. #include <QDebug>
    8.  
    9. class criaConfServidor : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. //explicit criaConfServidor(QObject *parent = 0, QString DBHost = "192.168.0.1", QString DBPorta = "5432");
    14. explicit criaConfServidor(QObject *parent = 0);
    15. void connectaThreadComSlot(QThread &processo);
    16.  
    17. signals:
    18.  
    19. public slots:
    20. void criaArquivoConf();
    21.  
    22. };
    23.  
    24. #endif // CRIACONFSERVIDOR_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by guidupas; 18th February 2014 at 22:17. Reason: spelling corrections

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem to implement a QThread

    You destroy the thread object after starting it, i.e. the variable "processo" goes out of scope.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem to implement a QThread

    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.

  4. #4
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem to implement a QThread

    So, where it must be declared?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem to implement a QThread

    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.

  6. #6
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem to implement a QThread

    It worked. Thank you very much people. I created the Qthread in .h file and iniciated it in .cpp file.

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 15:49
  2. QThread Problem
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 8th July 2009, 11:14
  3. QThread and OpenMP on Mac problem
    By Debilski in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2009, 18:41
  4. Qthread Problem
    By sroger in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2009, 08:35
  5. QThread problem
    By MrShahi in forum Qt Programming
    Replies: 6
    Last Post: 15th July 2008, 13:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.