[Interest] Signals, slots before the event loop starts?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Thu Apr 11 07:35:32 CEST 2019


Il 11/04/19 00:18, Jason H ha scritto:
> In a QObject who is exported to QML, and is instantiated just below the top-level Window:
> //  in the object's open() method:
> if (!_serialPort.open(QIODevice::ReadWrite))
> 	qApp->quit(); // won't actually quit - no use if I can't use the serial port. (because another instance is using it)
> 
> Then I have a ready() signal that is emitted when the serial device is ready, however the QML, when I hook onReady, it never gets called. I have to use a Component.onCompleted at the top level. However, there is async serial I/O happening, so there is at least one event loop?
> 
> What can I do to make sure these things work?

Regarding quit(): calling it before exec() has been called yields no 
effects. This is documented:

> https://doc.qt.io/qt-5/qcoreapplication.html#quit

> If a signal connected (non-queued) to this slot is emitted before control enters the main event loop (such as before "int main" calls exec()), the slot has no effect and the application never exits

So, instead of calling quit() directly, do a queued invocation.


Regarding ready(): can't say more without looking at the code. You need 
to make a minimal testcase.

Anyhow, all events queued before running exec() will get dispatched when 
you enter the event loop. The problem with e.g. quit() is that it does 
not involve sending events at all, it involves setting a flag into the 
event loop. Flag that is never read because the event loop is not running...

HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4007 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190411/882b5557/attachment.bin>


More information about the Interest mailing list