我正在 Qt 中创建一个简单的应用程序用于学习目的。我想显示网络摄像头捕获的图像,以显示在我的用户界面 (ui) 的图形 View 中。
功能:按下开始按钮时,网络摄像头帧开始显示在图形 View 中。当按下暂停按钮时,网络摄像头流式传输将暂停。最后,如果按下退出按钮,整个应用程序将终止。
我的方法:我想在按下开始按钮时启动一个 Qt 线程,该线程将连续捕获网络摄像头帧。捕获每个网络摄像头帧后,该线程将发出一个信号并将指针传递给捕获图像的数据、高度和宽度。此信号将连接到 MainWindow
类的插槽(即 setGraphicsView()
)。 MainWindow
类的 slot 会将捕获的网络摄像头图像分配给 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();
}
主窗口.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
主窗口.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
线程.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')
我是一名优秀的程序员,十分优秀!