• 8284阅读
  • 3回复

【提问】实时视频采集在多线程编程中实现的问题? [复制链接]

上一主题 下一主题
离线hitaeolus
 

只看楼主 倒序阅读 楼主  发表于: 2005-12-12
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
我使用QT designer作了一个程序,程序要求可以视频实时采集,我用一个窗口来显示摄像头捕捉的视频,我可以使用QThread类来实现这个视频采集,可是视频显示的程序是一个无限循环,这样Qt 的GUI 主事件线程就会一直在等待视频采集的结束,这样我的主界面mainform就不能运行了,主界面上的各个控件就会无响应。
如何才能让这个视频采集程序在后台运行,并在主程序里控制视频采集程序的开始和暂停?
请各位大侠帮忙!!!!
[ 此贴被XChinux在2005-12-12 14:40重新编辑 ]
离线wjydlut

只看该作者 1楼 发表于: 2005-12-12
你看看这个程序看能否有帮助 ,我以前好像也遇到过这样的情况 ,不过比你的简单
////////////////////////////////////////////////////////
#include <qapplication.h>
#include <qhbox.h>
#include <qthread.h>
#include <qlabel.h>


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("<h1>Done!</h1>");
  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, "<b>Ping</b>" );
  GUIThread second( &label, "<i>Pong</i>" );
 
  WaitThread third(&first, &second, &label);
  first.start();
  second.start();
 
  app.setMainWidget( &box );
  box.show();

 
  third.start();
 
  int r = app.exec();

  delete mutex;
  return r;
}
离线hitaeolus

只看该作者 2楼 发表于: 2005-12-13
多谢!!!
不过我的程序的摄像头捕捉的视频要求一直显示,就像在后台运行的程序一样,他是一个无线的循环,我觉得只能在它所在线程的run()函数里面用QWaitCondition::wait阻塞它来处理别的事情,不过具体怎么做还是没有头绪。
离线wjydlut

只看该作者 3楼 发表于: 2005-12-13
我前一段时间做的是一个线程不断的采集数据,然后在一个窗口显示.没有用你说的QWaitCondition::wait .
程序大概如下:
class Mythread::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!" );
    }
  }

再参照上面的程序大概就行了
快速回复
限100 字节
 
上一个 下一个