简体   繁体   English

强制更新Qt GUI

[英]Forcing the Qt GUI to update

I am coding a boardgame in Qt where, after the player makes a move, the computer AI must pause and think for some time. 我正在Qt中编写一个棋盘游戏,在该棋盘中,玩家进行移动之后,计算机AI必须暂停并思考一段时间。 However, while it is thinking, it seems the screen will not be updated until every line of code has been executed. 但是,在考虑时,似乎直到执行完每一行代码后,屏幕才会更新。 Thus, the user would click on a square, see nothing happen for a few seconds, and then suddenly see the result of both his move and the computer's move. 因此,用户将单击一个正方形,几秒钟后什么也看不见,然后突然看到他的移动计算机移动的结果。

In an attempt to fix this, I tried creating a new thread on which the AI runs its code, and then places its piece on the board. 为了解决此问题,我尝试创建一个新线程,AI在其上运行其代码,然后将其片段放置在板上。 However, sometimes (and this is very inconsistent) the game crashes after the computer has made a move. 但是,有时(这是非常不一致的),在计算机采取措施后,游戏会崩溃。

So can you guys either: 你们也可以:

  1. Tell me how force the MainWindow to update its contents 告诉我如何强制MainWindow更新其内容
  2. Help me try to debug the crashing problem (I'm totally new to threads) 帮助我尝试调试崩溃的问题(我是线程的新手)

EDIT --I tried setting breakpoints as suszterpatt suggested, and the program seems to crash consistently in the debugger (it wasn't before I set the breakpoints). 编辑 -我尝试按照suszterpatt的建议设置断点,并且该程序似乎在调试器中始终崩溃(不是在设置断点之前)。

Anyways, as I step through the program, it seems to go through the run function fine, until it reaches the ending bracket, and then if I step through it jumps into line 317 on qthread_win.cpp , which just says 无论如何,当我逐步执行程序时,它似乎可以很好地通过run函数,直到到达结尾括号为止,然后如果我逐步run该程序,则跳到qthread_win.cpp第317 qthread_win.cpp

finish(arg); //line 317
return 0;

If I step through that line, the debugger freezes up and Qt alerts me after 20 seconds. 如果我走那条线,调试器将冻结,并且Qt将在20秒后提醒我。 If I continue, I get the "This application has requested the Runtime to terminate it in an unusual way" message that I get when the program occasionally crashes when I'm not debugging. 如果我继续,则会收到“此应用程序已请求运行时以异常方式终止它的消息”消息,该消息是在我未调试时程序偶尔崩溃时收到的。

What should I do now? 我现在应该怎么办?

The cause of the crash can be a variety of reasons but if I had to take a guess I'd say you're probably calling methods of a GUI object (label, textbox, your game board, etc.) from the AI thread. 崩溃的原因可能是多种原因,但是如果我不得不猜测,我可能会说您可能正在从AI线程调用GUI对象的方法(标签,文本框,游戏板等)。

The way threads communicate with one another in Qt is through a mechanism called signals and slots : the AI thread should expose a set of signals, ie 'beginThink', 'endThink', and the UI thread should register to those signals (with slots) and react accordingly. 在Qt中,线程相互通信的方式是通过一种称为信号和插槽的机制进行的:AI线程应公开一组信号,即'beginThink','endThink',UI线程应向这些信号注册(带有插槽)并做出相应的反应。 This is documented pretty throughly in the docs. 在文档中对此进行了详尽的记录。

Try moving your code out of the separate thread. 尝试将代码移出单独的线程。 Once you have it working you can try moving it back and you will know any issues are threading related. 一旦工作,您可以尝试将其移回,您将知道与线程相关的所有问题。 I think your updating issue will go away if it's all on the same thread. 我认为如果所有更新都在同一线程上,您的更新问题将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM