首页| 论坛| 消息

标题:【提问】实时视频采集在多线程编程中实现的问题?
作者:hitaeolus
日期:2005-12-12 11:16
内容:

我使用QT designer作了一个程序,程序要求可以视频实时采集,我用一个窗口来显示摄像头捕捉的视频,我可以使用QThread类来实现这个视频采集,可是视频显示的程序是一个无限循环,这样Qt 的GUI 主事件线程就会一直在等待视频采集的结束,这样我的主界面mainform就不能运行了,主界面上的各个控件就会无响应。
如何才能让这个视频采集程序在后台运行,并在主程序里控制视频采集程序的开始和暂停?
请各位大侠帮忙!!!!


#1 [wjydlut 12-12 18:40]
你看看这个程序看能否有帮助 ,我以前好像也遇到过这样的情况 ,不过比你的简单
////////////////////////////////////////////////////////
#include
#include
#include
#include

class WaitThread: public QThread
{
public:
WaitThread(QThread *thr1, QThread *thr2, QLabel *lbl);
void run();
private:
QThread *thread1, *thread2;
QLabel *label;
};

WaitThread::WaitThread(QThread *thr1, QThread *thr2, QLabel *lbl)
: thread1(thr1), thread2(thr2), label(lbl)
{
}

void WaitThread::run()
{
thread1->wait();
thread2->wait();
qApp->lock();
label->setText("Done!");
qApp->unlock();
}
class GUIThread : public QThread
{
public:
GUIThread( QLabel*, const QString& );
protected:
void run();
private:
QLabel* label;
QString text;
};
static QMutex* mutex;
GUIThread::GUIThread( QLabel* l, const QString& t )
: label( l ), text( t )
{
}
void GUIThread::run()
{
for ( int i = 0; i < 2; i++ ) {
mutex->lock();
qApp->lock();
label->setText(text);
qApp->unlock();
sleep( 1 );
mutex->unlock();
}
}
// The program starts here
int main( int argc, char** argv )
{
QApplication app( argc, argv );
mutex = new QMutex;
QHBox box( 0, 0, TRUE );
QLabel label( &box );
label.setAlignment(Qt::AlignCenter);
label.setMinimumSize(400, 300);

GUIThread first( &label, "Ping" );
GUIThread second( &label, "Pong" );

WaitThread third(&first, &second, &label);
first.start();
second.start();

app.setMainWidget( &box );
..
#2 [hitaeolus 12-13 10:19]
多谢!!!
不过我的程序的摄像头捕捉的视频要求一直显示,就像在后台运行的程序一样,他是一个无线的循环,我觉得只能在它所在线程的run()函数里面用QWaitCondition::wait阻塞它来处理别的事情,不过具体怎么做还是没有头绪。
#3 [wjydlut 12-13 17:08]
我前一段时间做的是一个线程不断的采集数据,然后在一个窗口显示.没有用你说的QWaitCondition::wait .
程序大概如下:
classMythread::wait : public QThread {
Mythread(Qlabel *label);
public:
virtual void run();
};
MyThread::MyThread(QLabel* l ): label( l)
{
}
void MyThread::run()
{
for( ; ;) {
sleep( 1 );
label->setText("ping");
qDebug( "Ping!" );
}
}
再参照上面的程序大概就行了

回复 发表
主题 版块