Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Qt's meta object compiler (moc, a code generator that lets C++ kind of emulate Objective-C's message passing) is mighty handy for transparently queuing (instead of directly calling) signals that cross thread boundaries. I don't see anything in this post that mentions it.

See the relevant bit from Qt's documentation for more information: http://qt-project.org/doc/qt-5/why-moc.html



The transparent queueing is implemented by the Qt library (with QObject/QEventLoop). Technically, queueing does not rely on moc. (moc is needed for implementing signals)


This is a Qt development (i.e. the development of Qt, not with) blog so for their regular audience the reasons for moc existing are probably already well known.

It would be great if we could ditch moc. If you're using Qt without qmake it can be pain in the butt.


CMake has robust moc and Qt build support in general. I have put it through some interesting use cases without issue.

For a simple GUI program, in your CMakeLists.txt, you'd typically want something like: set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) find_package(Qt5Core REQUIRED) find_package(Qt5Gui REQUIRED) find_package(Qt5Widgets REQUIRED) qt5_wrap_ui(UI_HEADERS dialog1.ui dialog2.ui mainwindow.ui) qt5_add_resources(UI_RESOURCES resources.qrc) add_executable(simple_program main.cpp dialog1.cpp dialog2.cpp ${UI_HEADERS} ${UI_RESOURCES}) target_link_libraries(simple_program Qt5::Core Qt5::Gui Qt5::Widgets)

Additionally, CMake is the best way I have found to build PyQt5 Python extensions that have C++ Qt bits exposed via PyQt/SIP. A project (of mine) that does this: https://github.com/erikhvatum/RisWidget (MIT license, so feel free to copy and paste anything useful).


Message passing across threads is very easy with boost asio: you just post a callback to the respective io_service. That is effectively what Qt does anyway, but no Qt required.


Except when you need to pass a message to the GUI thread (main thread).


Why is that hard? With Qt, it's fairly straightforward: http://ix.io/flo

Similar things can be used for the various event loops.

If you were so inclined, you could write something that uses less dynamic memory allocation.

Now that I read that code, I think I should be moving some of this stuff to C++11, at least WRT rvalue-refs...


wxWidgets has QueueEvent and ProcessEvent and AddPendingEvent for this sort of thing - does Qt force you to emit a signal without being able to queue it first?


Qt offers both signals/slots and a lower level (but still cross platform) event system with the usual post event (async) and send event (blocking) methods.

Signals/slots provide a connection abstraction on top of events, and blocking vs queued behavior is a per-connection attribute. So, in cases where the emitter wants to control blocking vs async behavior, you might use the event system with post or send directly.



Transparent queuing has little to do with moc. It's to do with Qt's threading model and eventing.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: