Qt - emit a signal from a c++ thread

后端 未结 4 658
孤独总比滥情好
孤独总比滥情好 2021-01-01 20:04

I want to emit a signal from a C++ thread (std::thread) in Qt.

How can I do it?

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 20:51

    You probably should not emit a Qt signal from a std::thread-created thread in general without care. See Jpo38's answer : connection type matters, etc...

    If the thread is running some Qt event loop, you probably could. See threads and QObject

    There is a (Unix-specific probably) work-around, doing the same as for Unix signals with Qt : use a pipe from your std::thread to the main thread.

    But, as commented by Joachim Pileborg, you should make your own QThread. It is the simplest, and probably the shortest (in term of source code), and you just need to copy and paste some existing example and adapt it to your need.

    Beware that AFAIK only the main thread should do Qt GUI manipulations. You should not use any QWidget (etc...) outside of the main thread! (BTW, GTK has the same restriction, at least on Linux: only the main thread is supposed to use the X Windows system protocols)

提交回复
热议问题