Qthread信号发出未连接

Qthread signal emit not connecting

我有两个信号正在连接,一个没有。

我有一个主窗口小部件来显示由 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 无法连接。

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

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

在您的 UI 中,FPS 看起来像 QPlainTextEdit。我没有看到为 class 记录的 setNum() 插槽。 QLabel 有一个(它需要一个 int 或 double),这就是你想要使用的。