QRunnable中的QBuffer可能会也可能不会生成错误QObject :: moveToThread:当前线程不是对象的线程(QBuffer in a QRunnable may or may

编程入门 行业动态 更新时间:2024-10-24 22:29:04
QRunnable中的QBuffer可能会也可能不会生成错误QObject :: moveToThread:当前线程不是对象的线程(QBuffer in a QRunnable may or may not generates error QObject::moveToThread: Current thread is not the object's thread)

我有一个来自QRunnable对象的派生类。 它正在进行小型图像处理任务。 看起来如下:

void ImageProcessor::run() { int bytesPerLine; if(_format == QImage::Format_RGB32) bytesPerLine = _width * 4; QImage img(_buffer, _width, _height, bytesPerLine, _format); QTransform transform; transform.rotate(90); QImage rotatedImage = img.transformed(transform); _resultImage->clear(); QBuffer buffer(_resultImage); buffer.open(QIODevice::WriteOnly); rotatedImage.save(&buffer, "JPG", 85); buffer.close(); }

我的问题是当我执行我的应用程序时,显示以下错误消息,但并非总是如此。 我可以在其他时间运行它,看到没什么特别的。

QObject::moveToThread: Current thread (0x2012268) is not the object's thread (0x2007dc0). Cannot move to target thread (0x21a6218)

我使用的唯一QObject派生类是QBuffer,这个在执行线程中本地创建。 我不明白为什么我会这样。

任何帮助,将不胜感激!

如果可以提供任何线索,我在QT 5.3,Windows 7和MSVC2010 32位下运行。

以下指向重新创建问题的示例应用程序的链接。 ParallelImageProc.zip它包含一个屏幕截图,显示了我得到的实际结果。

谢谢!

I have a derived class from a QRunnable object. It is doing small image processing tasks. It looks like follow:

void ImageProcessor::run() { int bytesPerLine; if(_format == QImage::Format_RGB32) bytesPerLine = _width * 4; QImage img(_buffer, _width, _height, bytesPerLine, _format); QTransform transform; transform.rotate(90); QImage rotatedImage = img.transformed(transform); _resultImage->clear(); QBuffer buffer(_resultImage); buffer.open(QIODevice::WriteOnly); rotatedImage.save(&buffer, "JPG", 85); buffer.close(); }

My problem is when I execute my application, the following error message is displayed but not always. I may run it onother time and see nothing special.

QObject::moveToThread: Current thread (0x2012268) is not the object's thread (0x2007dc0). Cannot move to target thread (0x21a6218)

The only QObject derived class I use is QBuffer and this one is created locally in the executing thread. I don't understand why I'm getting this.

Any help would be appreciated!

I run this with QT 5.3, under Windows 7 and MSVC2010 32bits if it can gives any clues.

The following link to a sample application that recreates the issue. ParallelImageProc.zip It contains a screenshot that show the actual result that I'm getting.

Thanks!

最满意答案

我终于找到了问题,我正在回答我自己的问题,以防它可以帮助别人。

所以,问题与我使用QBuffer对象的事实无关。 我尝试执行类似于以下操作的内容,并且未记录任何错误消息:

QBuffer buffer; buffer.open(QBuffer::WriteOnly); char buf[] = new char[100 * 1024 * 1024; buffer.write(buf, 100 * 1024 * 1024); delete [] buf; buffer.close();

这使我感到困惑,因为从QObject派生的唯一类是QBuffer。 同时,我确信问题是由电话引起的:

rotatedImage.save(&buffer, "JPG", 85);

经过多次尝试并深入研究了Qt源文件,我发现自己错了。 还有另一个继承QObject的类。 使用场景后面的插件管理图像格式。 产生我问题的类是QImageIOPlugin。 在Qt源代码中,这些类是从专用于图像格式的QFactoryLoader实例加载的。

以下线程 ,即使它是针对不同的问题,帮助我直接指出我显示的错误消息的来源。

由于无法直接访问此工厂加载的实例,解决此问题的最简单方法是在使用任何线程之前将图像伪造成所需的格式。 在我的例子中,我只是在main(...)函数中添加了以下行:

QImage image(10, 10, QImage::Format_RGB32); QBuffer buffer; buffer.open(QBuffer::WriteOnly); image.save(&buffer, "JPG", 85); buffer.close();

在这种情况下,将在线程之前创建QImageIOPlugin。 将图像保存到QBuffer中没有错误信息也没有问题。

希望这可以帮助别人。 干杯!!

I finally found the problem and I'm answering my own question in case it could help someone else.

So, the problem was not related to the fact that I was using a QBuffer object. I tried to do something similar to the following and no error messages was logged:

QBuffer buffer; buffer.open(QBuffer::WriteOnly); char buf[] = new char[100 * 1024 * 1024; buffer.write(buf, 100 * 1024 * 1024); delete [] buf; buffer.close();

This made me perplex since the only class that derives from QObject was QBuffer. At the same, I was sure that the problem was caused by the call:

rotatedImage.save(&buffer, "JPG", 85);

After many tries and had a deep look into Qt source files, I figured out that I was wrong. There is still another class that inherits QObject. Image formats are managed using plugins behind the scene. The class that was generating me problems was QImageIOPlugin. In Qt source, those classes are loaded from a QFactoryLoader instance dedicated to image formats.

The following thread, even if it was for a different issue, helped me to point out directly at the origin of my displayed error message.

Since there is no way to directly access this factory loaded instance, the easiest way to solve the issue is to fake an image save into the desired format before using any threads. In my case, I simply added following lines in my main(...) function:

QImage image(10, 10, QImage::Format_RGB32); QBuffer buffer; buffer.open(QBuffer::WriteOnly); image.save(&buffer, "JPG", 85); buffer.close();

In such a case, the QImageIOPlugin will be created before threads. There will be no error message and no problem in saving images as well into a QBuffer.

Hope this may help someone else. Cheers!!

更多推荐

本文发布于:2023-08-05 22:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   可能会   也可   能不   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!