Solved Emitting a signal from another thread does not work
-
Hi all,
I have Qt application, where I have multiple boost::threads running inside. I provide a std::function to each thread to callback a string to be added to my log window. In this function I only have one line emitting the LogString signal with the message as parameter.
Somehow this does not work. It simply doesn't fire.
My setup of the signal is this:
connect(this, SIGNAL(SignalLogString(std::string)), this, SLOT(LogString(std::string)), Qt::QueuedConnection);
And the function that I provide to my threads is this:
auto funcSignalLog = [&](std::string str) { emit this->SignalLogString(str); //LogString(str); };
The above does not result in the LogString method in the main app to be called. However if I comment in the
LogString(str)
It does but results in an exception every now and when, I guess since it's called directly from a different thread.
Can any one shed light into how this can be resolved?
Best regards,
Carsten -
@panch
Arestd::string
s acceptable to be passed across threads safely? Does your calling function allow thestd:string
to go out of scope? Are you supposed to use either native C++char []
or evenQString
for this? -
auto goSlot = [=](){ emit mySignal((std::string)("nni")); }; connect(ui->pushButton, &QPushButton::clicked, [=](){ goSlot(); }); connect(this, &MainWindow::mySignal, this, &MainWindow::mySlot); void MainWindow::mySlot(std::string arg) { qDebug() << QString::fromStdString(arg); }
signals: void mySignal(std::string); private slots: void mySlot(std::string arg);
Its work fine.. I have Qt 5.5
-
I tried to replace the Signal and slot with QString instead of std::string - and it worked!
So somehow this doesn't work in threads, but aparrently it does when used in the GUI thread as stated above?Thanks anyway!