Qt signals and slots, threads, app.exec(), and related queries

吃可爱长大的小学妹 提交于 2019-11-30 09:34:08

If you don't call app.exec() then the program hits the end of your main() and ends. (Why? There's no more code to execute!)

app.exec() is an infinite loop of the following style:

do
{
  get event from system
  handle event
}
while (true);

If you use a queued connection, then the event is added to your event queue, and it will be performed at some point in the future during the app.exec() loop.

There is no second thread in your program. Events are delivered asynchronously by the OS, which is why it appears that there's something else going on. There is, but not in your program.

app.exec() enters the main event loop and waits until exit() is called.

update:
The main event loop and the glue code generated by qmake take care of transferring the event message from the QTcpServer to your ConnectionHandler.

If you'd use queued connections the actual connection to the QTcpServers slot would be delayed until the main event loop delivers the connection request.

When you say it enters an infinite loop, you mean it crashes the program?

Because listen() will become part of the main application event loop the way you have it set up, which runs until you exit the program. I'm not sure what the problem is. There should be no trouble of your signal being emitted in the main application event loop (exec()) whenever one is encountered.

If you like, you could have your ConnectionHandler class extend QThread and run listen() in it's own thread, apart from the main application loop.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!