[Qt-interest] MessageBox in thread

Thiago Macieira thiago at kde.org
Fri May 28 13:23:19 CEST 2010


Em Sexta-feira 28 Maio 2010, às 11:43:22, Vítězslav Košina escreveu:
> Hello,
> 
> I would like ask for help in this situation:
> 
> I have application, which in separate thread do some calculation and need
> sometime ask user for input extra value. I know, that all GUI operation can
> be done only in main thread.
> 
> I would like ask, what is standard way in Qt for this situation. Do QMutext
> in calculation thread, emit signal to slot in main thread a receive input
> from user in main thread ? How to receive this input tho calculation
> thread?
> 
> Thank you very much for explanation or example. On web is too much abstract
> discussion, but if is possible more better will be some cookbook or
> example.

Hi

The way to do this is more or less what you described: lock the calculation 
thread, emit a signal to a slot in the main thread and receive the input back.

A QMutex is not the proper mechanism here, though. You need a QSemaphore.

And to help you: Qt provides a connection mechanism which is exactly what 
you're looking for: QSemaphore-protected cross-thread delivery. It's the 
BlockingQueuedConnection type.

Just make your connection with that type and your calling thread (the 
calculation thread) will block until the slot returns, with its result stored 
somewhere.

Alternatively, QMetaObject::invokeMethod can take that argument type too.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100528/1dccaea5/attachment.bin 


More information about the Qt-interest-old mailing list