微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

QT 5.15.2 QImage SegmentationFault

如何解决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

这是我的抄送课:

#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 ?? ()

它在 raspBerry pi 4 中失败了。我在其他 linux 发行版中尝试了这段代码并且工作正常。

解决方法

QT C++ How to return QImage from QThread and display it on QLabel in QMainWindow? 的欺骗 - 请阅读 QImage ctor 告诉您的内容:缓冲区必须在 QImage 的整个生命周期中保持有效

您的数据之前超出范围(我假设您的信号位于不同的线程中)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。