在Qt线程中没有检测到信号和插槽

时间:2015-10-04 20:33:25

标签: c++ multithreading qt opencv

我正在Qt中创建一个用于学习目的的简单应用程序。我想显示网络摄像头捕获的图像,以便在我的用户界面(ui)的图形视图中显示。

功能:按 开始 按钮后,网络摄像头帧开始显示在图形视图中。按下暂停按钮后,网络摄像头流将暂停。最后,如果按下退出按钮,那么整个应用程序将被终止。

我的方法:我想在按下开始按钮时启动Qt线程,此线程将连续捕获网络摄像头帧。捕获每个网络摄像头帧后,该线程将发出信号并将指针传递给捕获图像的数据,其高度和宽度。该信号将连接到setGraphicsView()类的时隙(即MainWindow)。 MainWindow类的插槽会将捕获的网络摄像头图像分配给ui的GraphicsView。

我的代码:

的main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow *w = new MainWindow;
    w->show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

//Threading
#include "thread.h"

//OpenCV
#include <opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/opencv.hpp"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    void on_StartButton_clicked();

    void on_PauseButton_clicked();

    void on_QuitButton_clicked();
    void setGraphicsView(unsigned char* ptrWebcamImg, int iw, int ih);

private:
    Ui::MainWindow *ui;
    Thread guiThread;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsPixmapItem>

using namespace cv;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->StartButton, SIGNAL(clicked()), &guiThread, SLOT(start()), Qt::QueuedConnection);
    //When the thread emits a signal saying it has got a new webcam frame, reflect that change in the graphicsview of ui.
    QObject::connect(&guiThread,SIGNAL(signalGotNewFrame(const unsigned char* ptrWebcamImage, int iw, int ih)),this,SLOT(setGraphicsView(const unsigned char* ptrWebcamImage, int iw, int ih)), Qt::QueuedConnection);
    QObject::connect(ui->PauseButton, SIGNAL(clicked()), &guiThread, SLOT(stopRunning()), Qt::QueuedConnection);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setGraphicsView(unsigned char *ptrWebcamImg, int iw, int ih)
{
    Mat frame(ih, iw, CV_8UC3, Scalar(0,0,255));

    //Conver the Mat frame into QImage and assign it to the GraphicsView of ui.
    QImage image( frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888 );
    QGraphicsScene* scene = new QGraphicsScene();
    QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
    scene->addItem(item);
    ui->graphicsView->setScene(scene);
}

void MainWindow::on_StartButton_clicked()
{
}

void MainWindow::on_PauseButton_clicked()
{
    //guiThread.stopRunning();
}

void MainWindow::on_QuitButton_clicked()
{
    connect(ui->QuitButton,SIGNAL(released()), qApp, SLOT(quit()));
}

thread.h

#ifndef THREAD_H
#define THREAD_H

#include <QThread>

class Thread : public QThread
{
    Q_OBJECT

public slots:
    void stopRunning();
    //virtual void run();
protected:
    virtual void run();
private:
    bool isWebcamNeeded;
signals:
    void signalGotNewFrame(unsigned char* ptrFrame, int iw, int ih);

};


#endif // THREAD_H

thread.cpp

#include "thread.h"

//Qt and
#include <QGraphicsPixmapItem>
#include <iostream>

//OpenCV related
#include <opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;

void Thread::run()
{
    std::cout<<"\nIn the thread"<<std::endl;
    isWebcamNeeded = true;
    cv::VideoCapture cap(0);
    Mat frame;
    unsigned char *ptrFrame;
    while(isWebcamNeeded == true) //"this->isWebcamNeeded" will become false when PauseButton will be clicked
    {
        cap >> frame;
        ptrFrame = frame.data;
        emit signalGotNewFrame(ptrFrame, frame.cols, frame.rows);
    }
}

void Thread::stopRunning()
{
    isWebcamNeeded = false;
}

问题:我收到以下运行时错误:

QObject::connect: No such signal Thread::signalGotNewFrame(const unsigned char* ptrWebcamImage, int iw, int ih) in ../MyCamApp/mainwindow.cpp:12
QObject::connect:  (receiver name: 'MainWindow')

1 个答案:

答案 0 :(得分:0)

这段代码有很多问题!

  1. 这里不需要线程。 QTimer是更好更安全的解决方案(你会减少错误)
  2. 子类QThread is bad approach when handling threads(我不会解释原因,因为您不应该使用它们。)
  3. 你的信号是插槽被严格定义,什么会导致内存管理问题。
  4. cv::MatQPixmap的转换方法为a bit more complicated
  5. 您尝试在QGraphicsView上显示的方式完全错误。你应该read documentation how to use this part of Qt
  6. 最重要的是,你可以让事情正常运作。 IMO你应该采取一些简单的步骤开始。现在你在很多领域缺乏技能(至少3个),这对你来说非常痛苦。提高当时一个新领域的技能。