简体   繁体   English

如何确保Qt Widget.repaint已完成运行?

[英]How to make sure Qt Widget.repaint has finished running?

I am currently having a problem with Qt graphics view framework namely, I want to clear my QGraphicScene background color and then run a function to take a webcam picture. 我目前在使用Qt图形视图框架时遇到问题,即我想清除QGraphicScene背景色,然后运行一个功能来拍摄网络摄像头图片。 So far when I use QWidget.repaint the screen only got repaint after about 1 second and by then the camera function has been called and the image captured is always off. 到目前为止,当我使用QWidget.repaint时,屏幕仅在大约1秒钟后重新绘制,然后调用了照相机功能,并且捕获的图像始终处于关闭状态。 Here is how my code currently look like. 这是我的代码当前的样子。

//Scene is a QGraphicScene
//View is a QGraphicView
//Camera is a camera object
Scene.setBackgroundBrush(Qt::Blue)
View.repaint()
Camera.Capture()

I have tried wrapping the repaint() call with another function and use signal and slot call but it still fail. 我尝试用另一个函数包装repaint()调用,并使用信号和插槽调用,但仍然失败。 I want to know if there is a way to pause the program until the screen has been refreshed. 我想知道是否有办法在刷新屏幕之前暂停程序。

A QGraphicsView has a bit more going on than most QWidget subclasses and I'm not familiar enough with it to say what is going on for sure but I might venture a guess that your problem is related to the fact that the scene is actually rendered onto the view port widget. QGraphicsView比大多数QWidget子类有更多的功能,我对它的定义还不够熟悉,无法肯定发生了什么,但是我可以冒险猜出您的问题与场景实际渲染到屏幕上有关视口小部件。 Perhaps calling viewport->repaint() will give you the results you are looking for? 也许调用viewport->repaint()会给您想要的结果?

Also, unless you really need to be using the webcam in this scenario, you could call ::render() on your scene and pass it a QImage which you could save directly to a file. 另外,除非在这种情况下确实需要使用网络摄像头,否则可以在场景上调用::render()并将其传递给QImage ,然后将其直接保存到文件中。

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

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