Qthread信号发射不连接

时间:2018-02-18 02:58:23

标签: c++ qt signals-slots qthread

我有两个连接的信号,另一个不连接的信号。

我有一个主窗口窗口小部件来显示由Qthread处理的图像,它将原始图像和处理过的图像发送到我的mainwidow,显示它们,这是有效的。我还希望从这个线程发出每秒帧数,但它不会连接。

这里的所有代码: https://github.com/ianzur/qtGui_imageProcessing

mainwindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

// Timer for UI responsivness
tmrTimer = new QTimer(this);
connect(tmrTimer,SIGNAL(timeout()),this,SLOT(UpdateGUI()));
tmrTimer->start(20);

// Set initial sliders postion (for selecting ROI)
ui->SldLower->setSliderPosition(0);
ui->SldUpper->setSliderPosition(480);
ui->SldLeft->setSliderPosition(0);
ui->SldRight->setSliderPosition(640);

//connections to video processor thread

const bool c = connect(&processor,SIGNAL(outFPS(float)),ui->FPS,SLOT(setNum(float)));
connect(&processor,SIGNAL(inDisplay(QPixmap)),ui->inVideo,SLOT(setPixmap(QPixmap)));
connect(&processor,SIGNAL(outDisplay(QPixmap)),ui->outVideo,SLOT(setPixmap(QPixmap)));


qDebug() << "connected" << c;
}

线程声明

    class ProcessorThread : public QThread
    {
  Q_OBJECT


  public:
  explicit ProcessorThread(QObject *parent = 0);
  void update(QRect r, float low, float high);
  int CLOCK();
  float avgfps();

  signals:
  void inDisplay(QPixmap pixmap);
  void outDisplay(QPixmap pixmap);
  void outFPS(float fps);

  protected:
  void run() override;

  private:
  cv::VideoCapture capture;

  //Region of interest
  cv::Rect myROI;

  float lowHz;
  float highHz;
  //float fps;
  int _fpsstart=0;
  float _avgfps=0;
  float _fps1sec=0;

  };

线程定义

    ProcessorThread::ProcessorThread(QObject *parent) : QThread(parent)
 {

}

int ProcessorThread::CLOCK()
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC,  &t);
return (t.tv_sec * 1000)+(t.tv_nsec*1e-6);
}

float ProcessorThread::avgfps()
{
if(CLOCK()-_fpsstart>1000)
{
    _fpsstart=CLOCK();
    _avgfps=0.9*_avgfps+0.1*_fps1sec;
    _fps1sec=0;
}

_fps1sec++;
return _avgfps;
}

void ProcessorThread::run()
{
VideoCapture camera(0);

cv::Mat inFrame, outFrame, cropped, bit;

while(camera.isOpened() && !isInterruptionRequested())
{
    camera >> inFrame;

    if(inFrame.empty())
        continue;

    outFrame = inFrame.clone();

    cropped = inFrame(myROI);

    bitwise_not(cropped, bit);

    bit.copyTo(outFrame(myROI));

    //qDebug() << avgfps();

    emit outFPS(avgfps());

    emit inDisplay(QPixmap::fromImage(QImage(inFrame.data,inFrame.cols,inFrame.rows,inFrame.step,QImage::Format_RGB888).rgbSwapped()));

    emit outDisplay(QPixmap::fromImage(QImage(outFrame.data,outFrame.cols,outFrame.rows,outFrame.step,QImage::Format_RGB888).rgbSwapped()));


}
}

void ProcessorThread::update(QRect r, float low, float high)
{
this->myROI = cv::Rect(r.x(),r.y(),r.width(),r.height());

this->lowHz = low;
this->highHz = high;
}

我无法弄清楚为什么只有outFPS无法连接。

1 个答案:

答案 0 :(得分:1)

我的第一个想法,看着这一行:

const bool c = connect(&processor,SIGNAL(outFPS(float)),ui->FPS, SLOT(setNum(float)));

在您的用户界面中,FPS看起来像QPlainTextEdit。我没有看到为该课程记录的setNum()插槽。有一个用于QLabel(它需要一个int或double),就是你想要使用的。