PDA

View Full Version : QProgressBar and DB connection



guidupas
14th February 2014, 20:44
Hello!

I am with a problem to increment the QProgressBar before starting the DB connection. Cde below



void defConexao::criaArquivoConfServidor(QString host, QString porta)
{
funcoesDB = new bancoDados;
bool retorno = true;
QSqlDatabase DB;
caixaMensagens mensagem;

this->barraProgresso->setMaximum(2);
this->barraProgresso->setMinimum(0);
int valorProgresso = 0;

if(!DB.isOpen())
{
DB = funcoesDB->adicionarBancoDados(DB);
valorProgresso = 1;
this->barraProgresso->setValue(valorProgresso);
}

if(funcoesDB->conectar(DB, host, porta))
{
qDebug() << "Conectou";
funcoesDB->fechar(DB);

valorProgresso = 2;
this->barraProgresso->setValue(valorProgresso);
}
else
{
mensagem.fecharMensagem("erro", "Banco de dados", "Não foi possÃ*vel conectar ao banco de dados");
this->barraProgresso->reset();
retorno = false;
}
}


The problem is that the progress bar does not set the value of the first function before it try to connect, that occurs in the second function. It sets all the values just before trying to connect. Why is that?

Thank you a lot.

anda_skoa
15th February 2014, 13:26
The thread never gets a chance to process the events triggered by the value update, e.g. can't process the paint event that the progress requests.

Only after you return from the function and subsequently return to the event loop can it process those events, which is why you are seening the last state.

As an immediate thing you can call QCoreApplication::processEvents() after changing the progess value.

Cheers,
_

guidupas
15th February 2014, 13:50
Thank you very much