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.
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.