Qt UI线程问题 [英] Qt UI Threading Problems

查看:355
本文介绍了Qt UI线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我问了几个专门关于QFuture的问题,这是我当前用于将UI应用程序线程化的内容.

OK so i have asked a couple of questions specifically about QFuture which is what im currently using to thread my UI application.

基本上,我在一个类中有一个函数,该函数需要很长时间,所以自然地我希望它被线程化,因此当从我的UI类中调用时,它并不仅锁定了我的UI.

Basically i have a function in a class that takes a long time so naturally i wanted it threaded so when called from my UI class it didnt just lock up my UI.

然后,我实现了一个QFutureWatcher,以等待finish()信号,此时它将启动另一个在新线程中使用了很长时间的函数(但是此函数正在处理UI).我使用相同的代码来实现第二个线程函数,但是该函数仍然锁定了我的UI.

I then implemented a QFutureWatcher to wait for the finished() signal at which point it would start another function that took ages in a new thread (this one however was doing things to the UI). I used the same code to implement the second threaded function however this one still locks up my UI.

我得出的结论可能是完全错误的,但是我似乎无法找出为什么它不起作用,所以我所能做的就是假设并在这里询问...

i have come to conclusions which may be entirely wrong but i cant seem to find out why its not working so all i can do is assume and ask here...

也许我不能使用QConcurrentRun拥有2个QFuture

Maybe i cant have 2 QFuture's using QConcurrentRun

不能将QFuture与ui东西一起使用(在我的情况下,QGraphicsView添加到场景中)

Cant use QFuture with ui stuff (in my case QGraphicsView adding to scene)

如果有人有很好的建议,请iv阅读另一篇文章,我也许应该将QObject子类化,但是没有给出示例,所以我无法真正实现此目的,因为我不知道我打算重新实现什么样的功能.

if anyone has suggestions that would be great, iv read in another post i should maybe subclass QObject but no example was given so i couldnt really implement this because i didnt know what kind of functionality i was meant to even be re implementing ..

TLDR:如何使用对UI有用的功能(添加到QGraphicsScene中)

TLDR: HOW TO THREAD A FUNCTION THAT DOES THINGS TO UI (adding to QGraphicsScene)

代码示例,这是一个需要花费很长时间的函数,因为我从不同的函数循环调用该函数数十万次,大约需要30秒才能完成UI不会响应的时间

code sample, this is the function that takes ages because i call this function from a different functions loop a few hundred thousand times, takes about 30 seconds to complete in which time the UI is not responding

void GUI::paintSomething(double x, double y)
{
    /// Decalre a QPen for Painting dots
    QPen pen;

    // set the pen colour
    pen.setColor(Qt::white);

    // Add ellipse at the x y position passed in
    scene->addEllipse(x, y, 1.5, 1.5, pen, QBrush(Qt::SolidPattern));
}

线程代码,绘制全部为调用paintSomething的函数

Thread code, paint all being the function that calls paintSomething

*future2 = QtConcurrent::run(this, &GUI::paintAll);

// Set watcher to look at QFuture futre2
watcher2->setFuture(*future2);

推荐答案

首先更新ui/从除主线程之外的任何内容创建ui元素是一个很大的禁忌.因此,甚至不要走那条路.

Firstly updating the ui / creating ui elements from anything but your main thread is a big no-no. So don't even go down that route.

要在工作线程和主线程之间进行通信, 在Qt中,连接跨线程信号<->插槽时可以使用Qt::QueuedConnection.

To communicate between worker-threads and main threads, In Qt you can use a Qt::QueuedConnection when connecting cross-thread Signal <-> Slot.

您通常打算做的是将计算保存在工作线程中,并且当您要在QGraphicsView中创建元素并向其中添加元素时,发送带有所需相关信息的信号,将其捕获主线程并采取相应措施.

What you'd normally intend to do is have your calculations in the worker-thread and when your at the point to create and add elements to the QGraphicsView, send a signal with whatever relevant info is required, Catch it in the Main thread and act accordingly.

更新:

好吧,即使您调用任何事情数十万次",即使它是在大约30秒的时间内从主线程进行的单个函数调用,也可能会导致性能下降.正是它归结为.解决这个问题没有魔术,因为您受制于拥有的硬件以及从根本上在时钟周期内可以提供的硬件.

Ok if your calling anything "few hundred thousand times" even if it's a single function call from the main thread in a span of about 30 seconds, your going to have performance loss. It's just what it boils down to. There is no magic trick to solving that since your limited by what hardware you've got and what it can deliver in a clock-cycle fundamentally.

您需要以另一种方式解决您的问题.问问自己您的QGraphicsView正在呈现什么.

What you need is to address your problem in a different way. Ask yourself what your QGraphicsView is rendering.

也许您可以将其缓存到图像中吗?

Is it maybe things you can cache into an Image?

很少有十万个项目需要交互吗?

Do all Few hundred thousand items need to be interactable?

您还可以查看"Mandelbrot -Qt附带的示例" ,用于查看跨线程更新.

You can also have a look at the "Mandelbrot - Example" that comes with Qt to see cross-thread updates are handled.

这篇关于Qt UI线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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