请教如何在QT自定义线程类中使用QTimer定时器功能

时间:2021-10-03 19:13:39

代码贴上

VideoPlayThread.h:

class VideoPlayThread : public QThread{
 
private:
    QLabel * label_videoPlayer;
    QString fileName;
    CvCapture * g_capture;
 
    QImage * img;
    IplImage* frame;
public:
    VideoPlayThread();
    void run();
    void setFileName(QString FN);
    void setLabelVideoPlayer(QLabel * labelVP);
private slots:
    void nextFrame();
 
};
 
 
 
 
VideoPlayThread.cpp
 
VideoPlayThread::VideoPlayThread() : QThread()
{
//    timer = new QTimer;
//    connect(timer, SIGNAL(timeout()), this, SLOT(VideoPlayThread::nextFrame()));
}
 
void VideoPlayThread::setFileName(QString FN)
{
    fileName = FN;
}


void VideoPlayThread::setLabelVideoPlayer(QLabel * labelVP)
{
    label_videoPlayer = labelVP;
}
 
void VideoPlayThread::nextFrame()
{
 
    frame = cvQueryFrame(g_capture);
    if(frame)
    {
        cvCvtColor(frame, frame, CV_BGR2RGB);
        img = new QImage((uchar*)frame->imageData, (int)frame->width, (int)frame->height, (int)frame->widthStep, QImage::Format_RGB888);
        *img = img->scaledToWidth(label_videoPlayer->width(), Qt::FastTransformation);
        label_videoPlayer->setPixmap(QPixmap::fromImage(*img));
    }
//    else
//        timer->stop();
 
}
 
void VideoPlayThread::run()
{
    g_capture = NULL;
    QByteArray temp = fileName.toLatin1();
    char * FileName = temp.data();
    g_capture = cvCreateFileCapture(FileName);
    int frameNum = (int)cvGetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_COUNT);
    double fps = cvGetCaptureProperty(g_capture, CV_CAP_PROP_FPS);//获取帧率
    double vfps = 1000/fps;//计算每帧的播放时间
    frame = cvQueryFrame(g_capture);
    if(frame)
    {
        cvCvtColor(frame, frame, CV_BGR2RGB);
        img = new QImage((uchar*)frame->imageData, (int)frame->width, (int)frame->height, (int)frame->widthStep, QImage::Format_RGB888);
        *img = img->scaledToWidth(label_videoPlayer->width(), Qt::FastTransformation);
        label_videoPlayer->setPixmap(QPixmap::fromImage(*img));
        QTimer *timer = new QTimer;
        connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
        timer->start(30);
    }
    exec();
    cvReleaseCapture(&g_capture);
}
 调试的时候,发现问题好像是connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));这一句,提示信息是:No such slot QThread::nextFrame()。 

这个我槽函数定义在自定义的线程类VideoPlayThread里了,而且this也显示的是VideoPlayThread*类型,为什么connect函数要去QThread里找槽函数?

还有这一块应该怎么写啊。

小弟叩首求教。