Hi Guys,
I have test() function which is executed using QtConcurrent::run method in different thread. My main thread is GUI thread. When any signal is emmited from test() function, main thread does not catch that so i tried to execute slot OnTestSignal() which executes in main thread and emit signal from there. still the signal is not emmited.
For more detail i have documented code.
What am i doing wrong here ? i should be getting a signal but that not happeningCode:#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtConcurrentRun> #include <QCoreApplication> #include <QTimer> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // Run test in different thread QtConcurrent::run((*this),&MainWindow::test); QObject::connect(this,SIGNAL(testSignal(int)),ui->progressBar,SLOT(setValue(int)),Qt::QueuedConnection); /** * This signal is emmited and progress bar changes */ emit testSignal(qrand()%100); } MainWindow::~MainWindow() { delete ui; } void MainWindow::test() { qDebug() << "start :" << QThread::currentThread() ->signalsBlocked()<< " : " << thread()->signalsBlocked(); qDebug() << "Thread : " << QThread::currentThread() << " : " << thread(); int i; int j; while(1) { ++i; if(i > 10) { i =0; QMetaObject::invokeMethod(this,"onTestSignal",Qt::QueuedConnection); //emit testSignal(); /* DOES NOT WORK */ } } } void MainWindow::onTestSignal() { // Verify that test executed in different thread and this funtion is executed in main thread qDebug() << "onTestSignal() : " << QThread::currentThread() << " :Main Thread : " << thread(); /** * Executed in main thread still signal is not been emmited, WHY ???? */ emit testSignal(qrand()%100); // Did not work QCoreApplication::processEvents(); }
haws anybody faced similar issue?

Reply With Quote


