PDA

View Full Version : Qt Application becomes unresponsive after some functions



sogico
28th August 2012, 14:00
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?


#include "mainwindow.h"
#include "ui_mainwindow.h"




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

connect(ui->actionSalir, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionConfiguraci_n, SIGNAL(triggered()), this, SLOT(configurar()));

this->IP_from_config = "192.168.1.190";
estadoReady();


}



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


void MainWindow::configurar()
{
Dialog config;
config.exec();

this->IP_from_config = config.IP_address;
}



void MainWindow::inicializando()
{
//Crear colas de comunicación con RRC
socket_client = new CClientSocket();
socket_client->open_socket(this->IP_from_config.toAscii(),"5858");

//Esperar mensaje "RRC Ready"
int num_bytes;
bool b_wait = true;
do
{

num_bytes = socket_client->read_socket();
if(num_bytes > 0)
{
char* aux_msg = socket_client->get_string_from_buffer(num_bytes);
std::string recv_msg(aux_msg,num_bytes);
if (recv_msg.compare("RRC Ready") == 0)
b_wait = false;
}
usleep(1);

}while( b_wait);
std::cout << "Received RRC Ready" << std::endl;

}

void MainWindow::on_button_Conectar_clicked()
{
inicializando();


estadoConectando();
QCoreApplication::processEvents();

//Send Request
std::string message = "Connection Request";
bool resul;
do{
resul = socket_client->write_socket(message);
usleep(1);

}while(!resul);
std::cout << "Sent Connection Request" << std::endl;


//Esperar mensaje "Connection Complete"
std::string recv_msg;
int num_b;
bool b_wait = true;
do
{
num_b = socket_client->read_socket();
if(num_b > 0)
{
recv_msg = socket_client->get_string_from_buffer(num_b);
if (recv_msg.compare("Connection Complete") == 0)
b_wait = false;
}

usleep(1);

}while( b_wait);
std::cout << "Received Connection Complete" << std::endl;



estadoConectado();
QCoreApplication::processEvents();
}


void MainWindow::estadoInicial()
{
//Labels
ui->label->setText(QString::fromUtf8("Estado: Inicializando..."));
ui->label_2->setText("");

//Buttons
ui->button_Conectar->setVisible(false);

}

void MainWindow::estadoReady()
{
//Labels
ui->label->setText(QString::fromUtf8("Estado: Preparado."));
ui->label_2->setText("");

//Buttons
ui->button_Conectar->setVisible(true);

}

void MainWindow::estadoConectando()
{
//Start connection
ui->label->setText(QString::fromUtf8("Estado: Conectando..."));
ui->label_2->setText("");

//Update Buttons
ui->button_Conectar->setEnabled(false);


}

void MainWindow::estadoDesconectado()
{
//Labels
ui->label->setText(QString::fromUtf8("Estado: No conectado."));
ui->label_2->setText("");


ui->button_Conectar->setEnabled(true);

}

void MainWindow::estadoConectado()
{
//Labels
ui->label->setText(QString::fromUtf8("Estado: Conectado."));
ui->label_2->setText("");

//Buttons
ui->button_Conectar->setVisible(false);

}



Thank you very much.

tbscope
28th August 2012, 14:30
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.php?title=Multi_client_server_without_thread ing

sogico
29th August 2012, 09:19
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
MainWindow::inicializando() function, and I supose is because I have linked twice the button event to the slot
void MainWindow::on_button_Conectar_clicked(), 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?

high_flyer
29th August 2012, 12:39
your application probably hangs in your while loop:


bool b_wait = true;
do
{

num_bytes = socket_client->read_socket();
if(num_bytes > 0) // if no bytes are read, b_wait stays true, remaining in the loop.
{
char* aux_msg = socket_client->get_string_from_buffer(num_bytes);
std::string recv_msg(aux_msg,num_bytes);
if (recv_msg.compare("RRC Ready") == 0)
b_wait = false;
}
usleep(1);

}while( b_wait);