QT 5.15.2 QImage SegmentationFault



我正在处理一个我无法解决的错误。我试图显示后处理的相机图像从c++类QT组件,但我得到一个分割故障,我无法得到如何修复它的任何线索:

我的头文件:

#ifndef _CAMERA_VIEW_H_
#define _CAMERA_VIEW_H_
#include <QtQuick>
class CameraView : public QQuickPaintedItem
{
Q_OBJECT
QML_ELEMENT
Q_DISABLE_COPY(CameraView)
public:
explicit CameraView(QQuickItem* parent = nullptr);
void paint(QPainter *painter);
public slots:
void updateImage(const QImage&);
protected:
QImage m_image;
};
#endif

这是我的cc类:

#include "core/CameraView.hpp"
#include "core/moc_CameraView.cpp"
#include <spdlog/spdlog.h>
CameraView::CameraView(QQuickItem* parent):QQuickPaintedItem(parent){}
void CameraView::updateImage(const QImage &img)
{
qDebug() << Q_FUNC_INFO << "Image Valid, updating image...";
m_image = img.copy(); // does shallow copy of image data
update();             // triggers actual update
}

void CameraView::paint(QPainter *painter) {
if(m_image.isNull())
return;
qDebug() << Q_FUNC_INFO << "paint requested...";
QRectF bounding_rect = boundingRect();
QImage scaled = m_image.scaledToHeight(bounding_rect.height());
QPointF center = bounding_rect.center() - scaled.rect().center();
if (center.x() < 0)
center.setX(0);
if (center.y() < 0)
center.setY(0);
painter->drawImage(center, scaled);
}
下面是我的QML文件中使用这个组件的部分:
GridLayout {
id: plateDetector
objectName: "plateDetector"
columns: 1
rowSpacing: parent.height *.1
Layout.preferredHeight: parent.height
Layout.preferredWidth: parent.width

CameraView {
id: plateViewer
objectName: "plateViewer"
renderTarget: "FramebufferObject"
Layout.preferredWidth: parent.width
Layout.preferredHeight: parent.height *0.7
}
}

槽调用代码:

cvtColor(drawMat, drawMat, cv::COLOR_BGR2RGB);
QImage image= QImage((uchar*) drawMat.data, drawMat.cols, drawMat.rows, drawMat.step, QImage::Format_RGB888);
emit updateImage(image);
连接代码:
//Getting plate manager controller elements
QObject *plateManagerObject = findElement<QObject *>(objects, "plateManager");
if(!plateView){
QCoreApplication::exit(-1);
}
//Connect detector plate controller signals
PlateDetectorController plateController(db, nullptr);
spdlog::info("Plate Viewer loaded");
QObject::connect(&plateController, &PlateDetectorController::updateImage, plateView, &CameraView::updateImage);

我尝试用gdb调试它,但没有成功,也尝试了一些替代方法来复制图像并渲染图像,但这不起作用。

这里gdb输出:

pi@rasp:~/SmartGate/build $ gdb
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "armv7l-unknown-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file SmartGate
Reading symbols from SmartGate...
(gdb) r
Starting program: /home/pi/SmartGate/build/SmartGate 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
QML debugging is enabled. Only use this in a safe environment.
[New Thread 0xafc721e0 (LWP 17660)]
[2021-01-21 13:36:02.487] [info] Looking for scripts in folder resource/db/
[2021-01-21 13:36:02.488] [info] Running script in file resource/db/plate.sql
[2021-01-21 13:36:02.489] [info] Data base structure initialization done
[New Thread 0xae9b31e0 (LWP 17661)]
[New Thread 0xa7dc31e0 (LWP 17662)]
[2021-01-21 13:36:17.978] [info] Plate Viewer loaded
[New Thread 0xa71ff1e0 (LWP 17663)]
[New Thread 0xa55d31e0 (LWP 17664)]
[New Thread 0xa451d1e0 (LWP 17665)]
[New Thread 0xa3bff1e0 (LWP 17666)]
[New Thread 0xa31ff1e0 (LWP 17667)]
void CameraView::updateImage(const QImage&) Validating image...
void CameraView::updateImage(const QImage&) Image Valid, updating image...
Thread 1 "SmartGate" received signal SIGSEGV, Segmentation fault.
0xb6fb9bf0 in memcpy () from /usr/lib/arm-linux-gnueabihf/libarmmem-v7l.so
(gdb) bt
#0  0xb6fb9bf0 in memcpy () from /usr/lib/arm-linux-gnueabihf/libarmmem-v7l.so
#1  0x00000000 in ?? ()

它是失败的树莓派4。我在其他linux发行版中尝试了这段代码,并且工作正常。

QT c++如何从QThread返回QImage并在QMainWindow的QLabel上显示它?-请阅读QImage函数告诉你的:缓冲区必须在QImage的整个生命周期内保持有效

你的数据超出作用域之前(我假设你的信号是在不同的线程)

相关内容

  • 没有找到相关文章

最新更新