美文网首页
Qt Qthread使用

Qt Qthread使用

作者: Caiaolun | 来源:发表于2019-11-08 18:41 被阅读0次

    1. testopencvthread.h

    //做信号枪
    signals:
        void ImageProcessFinished(QImage image);
    

    2. testopencvthread.cpp

    //发信号,告诉ImageProcessFinished 我射了
    emit ImageProcessFinished(image);
    

    3. mainwindows.h

    //做靶子, 注定要给信号枪射的, 逃不掉的, 除非(该函数没在 slots 标志里面)
    public slots:
    void ShowImageFromThread(QImage image);
    

    4.mainwindows.cpp

    //建立轨道, 必须怀孕的轨道
    connect(m_pVideoCapture,SIGNAL(ImageProcessFinished(QImage)),
                       this,SLOT(ShowImageFromThread(QImage)));
    

    testopencvthread.h

    #ifndef TESTOPENCVTHREAD_H
    #define TESTOPENCVTHREAD_H
    
    #include <QWidget>
    #include <QThread>
    #include <QImage>
    
    #include "opencv2/opencv.hpp"
    
    using namespace cv;
    
    class TestOpencvThread : public QThread
    {
        Q_OBJECT
    public:
        explicit TestOpencvThread();
    
    public slots:
    
    signals:
        void ImageProcessFinished(QImage image);
    
    protected:
        void run();
        void InitVideoCapture();
    private:
        Mat m_srcImage;
        VideoCapture m_videoCap;
    };
    
    #endif // TESTOPENCVTHREAD_H
    

    testopencvthread.cpp

    #include "testopencvthread.h"
    TestOpencvThread::TestOpencvThread() :  QThread()
    {
        InitVideoCapture();
    }
    void TestOpencvThread::InitVideoCapture()
    {
        m_videoCap.open(0);
    }
    void TestOpencvThread::run()
    {
        QImage image;
        while(1)
        {
            m_videoCap>>m_srcImage;
    
            if(m_srcImage.data)
            {
                cvtColor(m_srcImage, m_srcImage, CV_BGR2RGB);//Qt中支持的是RGB图像, OpenCV中支持的是BGR
                image = QImage((uchar*)(m_srcImage.data), m_srcImage.cols, m_srcImage.rows, QImage::Format_RGB888);
            }
            emit ImageProcessFinished(image);
        }
    }
    

    mainwindows.h

    
    #include "testopencvthread.h"
    
    class DisplayWidget : public QWidget
    {
        Q_OBJECT
    public:
        explicit DisplayWidget(QWidget *parent = nullptr);
    
    signals:
    
    public slots:
        void ShowImageFromThread(QImage image);
    protected:
        void InitVideoCapture();
    private:
    
        TestOpencvThread *m_pVideoCapture;
        QLabel *m_pxx;
    };
    
    #endif // DISPLAYWIDGET_H
    
    

    mainwundows.cpp

    #include "displaywidget.h"
    
    DisplayWidget::DisplayWidget(QWidget *parent) : QWidget(parent)
    {
        InitVideoCapture();
        InitLayout();
    }
    void DisplayWidget::InitVideoCapture()
    {
        m_pVideoCapture = new VideoCaptureWidget;
        connect(m_pVideoCapture,SIGNAL(ImageProcessFinished(QImage)),this,SLOT(ShowImageFromThread(QImage)));
        m_pVideoCapture->start();
    }
    void DisplayWidget::ShowImageFromThread(QImage image)
    {
       QPixmap *pixmap = new QPixmap();
       pixmap->scaled(m_pxx->size(), Qt::KeepAspectRatio);
       m_pxx->setPixmap(pixmap->fromImage(image));
    }
    

    相关文章

      网友评论

          本文标题:Qt Qthread使用

          本文链接:https://www.haomeiwen.com/subject/ciuwbctx.html