PDA

View Full Version : WaitOrbusyDialog not updating corrcetly even after process events



anbu01
12th March 2016, 15:08
Hi,
I did a sample test for learning thread.I have the follwoing code but for saome reson the wait or busy dialog does not run properly.I have attached a video where you can see dialog initialize hangs
https://youtu.be/mjpnrLUx1VI.Here is the sample code:-


Main Window.cpp

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

QThread* thread = new QThread;
Worker* worker = new Worker();
m_dialog = new WaitOrBusyDialog; //A custom class to busy dialog

worker->moveToThread(thread);
connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)),Qt::QueuedConnection);
connect(thread, SIGNAL(started()), worker, SLOT(process()),Qt::QueuedConnection);
connect(worker,SIGNAL(showDialog()),this,SLOT(onDi alog()),Qt::QueuedConnection);
connect(worker, SIGNAL(finished()), thread, SLOT(quit()),Qt::QueuedConnection);
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()),Qt::QueuedConnection);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()),Qt::QueuedConnection);
thread->start();

}

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

void MainWindow::onDialog()
{
m_dialog->show();
}


WorkerThread.cpp


void Worker::process() {
qApp->processEvents();
int i=0;
emit showDialog();
while(i<10000)
{
qApp->processEvents();
qDebug("Hello World!");
i++;
}
emit finished();
}

This is just a sample code to know simultanoeusly backgroud (debug output) and ui foreground process.The problem is why initially dialog hangs.

anda_skoa
12th March 2016, 16:09
Don't call qApp->processEvents() in the worker.
It runs in a different thread than the main event loop.

There is also no need to call processEvent(), the main thread doesn't do anything other then event processing.

Cheers,
_

anbu01
12th March 2016, 17:04
@anda
After ignoring processevent still the dialog hangs.

anda_skoa
12th March 2016, 17:17
Maybe something else blocking the main thread.

Have you tried without starting the thread, e.g. just calling onDialog() using QTimer::singleShot()?

Cheers,
_