is there any limitation of how fast or how many times signals can be emitted in Qt? -
i experimenting qthread other day , wanted create infinite loop using signals , not for, foreach or while code crash after emitting signal , executing slot number of times here's code:
//mainwindow.h #ifndef mainwindow_h #define mainwindow_h #include <qmainwindow> #include "worker.h" #include <qthread> #include <qdebug> namespace ui { class mainwindow; } class mainwindow : public qmainwindow { q_object public: explicit mainwindow(qwidget *parent = 0); ~mainwindow(); void startthreads(); private: ui::mainwindow *ui; qthread thread; worker *work; }; #endif // mainwindow_h //worker.h #ifndef worker_h #define worker_h #include <qobject> #include <qdebug> #include <qmutex> #include "insiderobject.h" class worker : public qobject { q_object public: explicit worker(qobject *parent = 0); signals: void doagain(); void okidid(); void finished(); public slots: void printing(); }; #endif // worker_h //mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); startthreads(); } mainwindow::~mainwindow() { thread.wait(); delete ui; delete work; } void mainwindow::startthreads() { work = new worker; work->movetothread(&thread); connect(&thread, signal(started()), work, slot(printing())); connect(work, signal(finished()), &thread, slot(quit())); thread.start(); } //worker.cpp #include "worker.h" worker::worker(qobject *parent) : qobject(parent) { connect(this, signal(okidid()), this, slot(printing())); } void worker::printing() { qdebug() << "printing"; emit okidid(); } //main.cpp #include "mainwindow.h" #include <qapplication> int main(int argc, char *argv[]) { qapplication a(argc, argv); mainwindow w; w.show(); return a.exec(); }
i have read entire documentation signal/slots , threads , queued connections or whatever hands on not understand reason crash... tried chatting people , developers @ qt irc chat room no 1 tell me reason.
what doing infinite recursion. emit okidid();
direct call worker::printing()
. cause stack overflow.
you can fix using queued signal connection:
connect(this, signal(okidid()), this, slot(printing()), qt::queuedconnection);
now emit okidid();
not direct function call anymore. worker::printing()
function called in qt's event loop.
Comments
Post a Comment