Results 1 to 4 of 4

Thread: Qt Application becomes unresponsive after some functions

  1. #1
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt Application becomes unresponsive after some functions

    Hi all,

    I've developed a GUI application in which when you click on a button, you start a connection process, opening a socket, waiting for a "Ready" message, sending a "Connection Resquest" and waiting for a "Connection Complete". The connection process is ended when you received the last message, very simple. Moreover, the application has two items in the main menu, one of them choose the IP server of the socket and the other one closes the application.

    At first, the application runs correctly, the socket is opened and the messages are sent. When this process ends, the application has nothing to do, except it should respond if you click one of the two options in the menu. However, after 3 or 4 seconds the widget becomes darker and I cannot click on the menu. If I try to close directly the widget, a message appears saying: "Application does not respond".

    Any idea? Where is the error?

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5.  
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12.  
    13. connect(ui->actionSalir, SIGNAL(triggered()), this, SLOT(close()));
    14. connect(ui->actionConfiguraci_n, SIGNAL(triggered()), this, SLOT(configurar()));
    15.  
    16. this->IP_from_config = "192.168.1.190";
    17. estadoReady();
    18.  
    19.  
    20. }
    21.  
    22.  
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29.  
    30. void MainWindow::configurar()
    31. {
    32. Dialog config;
    33. config.exec();
    34.  
    35. this->IP_from_config = config.IP_address;
    36. }
    37.  
    38.  
    39.  
    40. void MainWindow::inicializando()
    41. {
    42. //Crear colas de comunicación con RRC
    43. socket_client = new CClientSocket();
    44. socket_client->open_socket(this->IP_from_config.toAscii(),"5858");
    45.  
    46. //Esperar mensaje "RRC Ready"
    47. int num_bytes;
    48. bool b_wait = true;
    49. do
    50. {
    51.  
    52. num_bytes = socket_client->read_socket();
    53. if(num_bytes > 0)
    54. {
    55. char* aux_msg = socket_client->get_string_from_buffer(num_bytes);
    56. std::string recv_msg(aux_msg,num_bytes);
    57. if (recv_msg.compare("RRC Ready") == 0)
    58. b_wait = false;
    59. }
    60. usleep(1);
    61.  
    62. }while( b_wait);
    63. std::cout << "Received RRC Ready" << std::endl;
    64.  
    65. }
    66.  
    67. void MainWindow::on_button_Conectar_clicked()
    68. {
    69. inicializando();
    70.  
    71.  
    72. estadoConectando();
    73. QCoreApplication::processEvents();
    74.  
    75. //Send Request
    76. std::string message = "Connection Request";
    77. bool resul;
    78. do{
    79. resul = socket_client->write_socket(message);
    80. usleep(1);
    81.  
    82. }while(!resul);
    83. std::cout << "Sent Connection Request" << std::endl;
    84.  
    85.  
    86. //Esperar mensaje "Connection Complete"
    87. std::string recv_msg;
    88. int num_b;
    89. bool b_wait = true;
    90. do
    91. {
    92. num_b = socket_client->read_socket();
    93. if(num_b > 0)
    94. {
    95. recv_msg = socket_client->get_string_from_buffer(num_b);
    96. if (recv_msg.compare("Connection Complete") == 0)
    97. b_wait = false;
    98. }
    99.  
    100. usleep(1);
    101.  
    102. }while( b_wait);
    103. std::cout << "Received Connection Complete" << std::endl;
    104.  
    105.  
    106.  
    107. estadoConectado();
    108. QCoreApplication::processEvents();
    109. }
    110.  
    111.  
    112. void MainWindow::estadoInicial()
    113. {
    114. //Labels
    115. ui->label->setText(QString::fromUtf8("Estado: Inicializando..."));
    116. ui->label_2->setText("");
    117.  
    118. //Buttons
    119. ui->button_Conectar->setVisible(false);
    120.  
    121. }
    122.  
    123. void MainWindow::estadoReady()
    124. {
    125. //Labels
    126. ui->label->setText(QString::fromUtf8("Estado: Preparado."));
    127. ui->label_2->setText("");
    128.  
    129. //Buttons
    130. ui->button_Conectar->setVisible(true);
    131.  
    132. }
    133.  
    134. void MainWindow::estadoConectando()
    135. {
    136. //Start connection
    137. ui->label->setText(QString::fromUtf8("Estado: Conectando..."));
    138. ui->label_2->setText("");
    139.  
    140. //Update Buttons
    141. ui->button_Conectar->setEnabled(false);
    142.  
    143.  
    144. }
    145.  
    146. void MainWindow::estadoDesconectado()
    147. {
    148. //Labels
    149. ui->label->setText(QString::fromUtf8("Estado: No conectado."));
    150. ui->label_2->setText("");
    151.  
    152.  
    153. ui->button_Conectar->setEnabled(true);
    154.  
    155. }
    156.  
    157. void MainWindow::estadoConectado()
    158. {
    159. //Labels
    160. ui->label->setText(QString::fromUtf8("Estado: Conectado."));
    161. ui->label_2->setText("");
    162.  
    163. //Buttons
    164. ui->button_Conectar->setVisible(false);
    165.  
    166. }
    To copy to clipboard, switch view to plain text mode 

    Thank you very much.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Qt Application becomes unresponsive after some functions

    That's expected behaviour since you use a lot of while loops and sleep commands. Effectively, you're blocking the event queue.

    The following article contains some pointers on keeping the event queue responsive:
    http://www.qtcentre.org/wiki/index.p...hout_threading

  3. #3
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Application becomes unresponsive after some functions

    Thank you, tbscope.

    But the application was still blocked when it finishes all those loops. I 've found that is because it executes twice the
    Qt Code:
    1. MainWindow::inicializando()
    To copy to clipboard, switch view to plain text mode 
    function, and I supose is because I have linked twice the button event to the slot
    Qt Code:
    1. void MainWindow::on_button_Conectar_clicked()
    To copy to clipboard, switch view to plain text mode 
    , but I don't know why if I didn't write a connect and connection signal - slot is made only by name, where is my mistake?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt Application becomes unresponsive after some functions

    your application probably hangs in your while loop:
    Qt Code:
    1. bool b_wait = true;
    2. do
    3. {
    4.  
    5. num_bytes = socket_client->read_socket();
    6. if(num_bytes > 0) // if no bytes are read, b_wait stays true, remaining in the loop.
    7. {
    8. char* aux_msg = socket_client->get_string_from_buffer(num_bytes);
    9. std::string recv_msg(aux_msg,num_bytes);
    10. if (recv_msg.compare("RRC Ready") == 0)
    11. b_wait = false;
    12. }
    13. usleep(1);
    14.  
    15. }while( b_wait);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 4
    Last Post: 4th September 2012, 19:28
  2. API functions in Qt?
    By Ali Reza in forum Newbie
    Replies: 3
    Last Post: 6th June 2012, 01:28
  3. C++ Static Functions
    By weaver4 in forum Newbie
    Replies: 7
    Last Post: 17th November 2009, 10:42
  4. Replies: 3
    Last Post: 4th March 2008, 08:35
  5. Using a frame for different functions
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 29th January 2008, 13:45

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.