how to emit signal inside constructor
-
How to emit signal inside constructor?
I have class that try to find files and analyse its. After last file done my class should emit mysignal(). If there is no any files I want too emit mysignal() in constructor...
This is wrong wayMyClass::MyClass(QObject *parent) : QObject(parent){ emit mysignal(); //Emitting inside constructor probably has no effect [clazy-incorrect-emit] }
I have idea. Using of QTimer::singleShot() is a good practice or dirty hack?
MyClass::MyClass(QObject *parent) : QObject(parent){ QTimer::singleShot(0, this, [&]{ emit finished(); }); }
-
How to emit signal inside constructor?
I have class that try to find files and analyse its. After last file done my class should emit mysignal(). If there is no any files I want too emit mysignal() in constructor...
This is wrong wayMyClass::MyClass(QObject *parent) : QObject(parent){ emit mysignal(); //Emitting inside constructor probably has no effect [clazy-incorrect-emit] }
I have idea. Using of QTimer::singleShot() is a good practice or dirty hack?
MyClass::MyClass(QObject *parent) : QObject(parent){ QTimer::singleShot(0, this, [&]{ emit finished(); }); }
@DungeonLords
As https://stackoverflow.com/questions/41220300/why-emit-signal-in-constructor-doesnt-work says, you won't have connected the signal while still inside the constructor so how should that work?You can use your "timer delay" to work around it if you want. Won't be emitted till next time event loop is hit.
I have class that try to find files and analyse its.
Maybe that's too much work to put in a constructor? You could avoid problems and timer if you move the finding/analyzing out of constructor and into some callable method?
-