强制Qt GUI进行更新 [英] Forcing the Qt GUI to update

查看:190
本文介绍了强制Qt GUI进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Qt编写一个棋盘游戏,在玩家移动之后,电脑AI必须暂停和思考一段时间。然而,在思考的过程中,似乎屏幕将不会被更新,直到每一行代码被执行。因此,用户将点击一个正方形,看不到任何事情发生几秒钟,然后突然看到他的动作计算机移动的结果。



为了解决这个问题,我尝试创建一个新的线程,AI运行它的代码,然后把它的棋子放在棋盘上。然而,有时(这是非常不一致的)游戏在电脑移动之后崩溃。



你们还可以:


  1. 告诉我如何强制MainWindow更新其内容

  2. 帮助我尝试调试崩溃的问题(我是全新的线程)

编辑 - 我尝试设置断点作为suszterpatt建议,程序似乎一直崩溃在调试器中(它不是在我设置断点之前)。



无论如何,当我浏览程序时,似乎通过运行函数,直到它到达结尾括号,然后如果我通过它跳转到第317行在 qthread_win.cpp ,这只是说

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

如果我通过该行,调试器冻结,Qt会在20秒后提醒我。如果我继续,我得到这个应用程序请求运行时以不寻常的方式终止它消息,当程序偶尔崩溃时,我没有调试。



我现在应该怎么做?

解决方案

崩溃的原因可能有多种原因,但如果我不得不猜测我会说你可能在AI线程中调用GUI对象(标签,文本框,游戏板等)的方法。



通过一个名为信号和插槽的机制,Qt中的双向线程彼此通信: AI线程应该公开一组信号,即'beginThink','endThink',UI线程应该注册到那些信号(带插槽)并作出相应的反应。这在文档中非常简单。


I am coding a boardgame in Qt where, after the player makes a move, the computer AI must pause and think for some time. 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. 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
  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).

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

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

If I step through that line, the debugger freezes up and Qt alerts me after 20 seconds. 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.

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, i.e. 'beginThink', 'endThink', and the UI thread should register to those signals (with slots) and react accordingly. This is documented pretty throughly in the docs.

这篇关于强制Qt GUI进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆