2013-07-12 4 views
0

Я использую Qt5.0.1 с Visual Studio 2010 интегрированы,Как добавить QWidget в макет во время выполнения Использование QThread?

Я рендеринг видео кадров из видео файла с помощью FFmpeg с Qt как QImage, на самом деле мне нужно отображать обработанный видеокадр во время выполнения с помощью QThread ,

Я новичок в Qt, пожалуйста, предоставьте несколько сиппетов.

Любые фрагменты будут полезны.

ответ

1

им не знакомы с VC++ с Qt ..но Использование mandle brot например

и фортуны пример резьбы enter link description here

используют сигналы, чтобы излучать массив видеокадра или QImage.

также этот фрагмент кода

#include <QImage> 
#include <QMutex> 
#include <QQueue> 
#include <QThread> 
#include <QWaitCondition> 

class Transaction 
{ 
public: 
    virtual ~Transaction() { } 

    virtual QImage apply(const QImage &image) = 0; 
    virtual QString message() = 0; 
}; 

class FlipTransaction : public Transaction 
{ 
public: 
    FlipTransaction(Qt::Orientation orientation); 

    QImage apply(const QImage &image); 
    QString message(); 

private: 
    Qt::Orientation orientation; 
}; 

class ResizeTransaction : public Transaction 
{ 
public: 
    ResizeTransaction(const QSize &size); 

    QImage apply(const QImage &image); 
    QString message(); 

private: 
    QSize size; 
}; 

class ConvertDepthTransaction : public Transaction 
{ 
public: 
    ConvertDepthTransaction(int depth); 

    QImage apply(const QImage &image); 
    QString message(); 

private: 
    int depth; 
}; 

class TransactionThread : public QThread 
{ 
    Q_OBJECT 

public: 
    TransactionThread(); 
    ~TransactionThread(); 

    void addTransaction(Transaction *transact); 
    void setImage(const QImage &image); 
    QImage image(); 

signals: 
    void transactionStarted(const QString &message); 
    void allTransactionsDone(); 

protected: 
    void run(); 

private: 
    QImage currentImage; 
    QQueue<Transaction *> transactions; 
    QWaitCondition transactionAdded; 
    QMutex mutex; 
}; 

и

#include <QtGui> 

#include "transactionthread.h" 

Transaction * const EndTransaction = 0; 

FlipTransaction::FlipTransaction(Qt::Orientation orientation) 
{ 
    this->orientation = orientation; 
} 

QImage FlipTransaction::apply(const QImage &image) 
{ 
    return image.mirrored(orientation == Qt::Horizontal, 
          orientation == Qt::Vertical); 
} 

QString FlipTransaction::message() 
{ 
    if (orientation == Qt::Horizontal) { 
     return QObject::tr("Flipping image horizontally..."); 
    } else { 
     return QObject::tr("Flipping image vertically..."); 
    } 
} 

ResizeTransaction::ResizeTransaction(const QSize &size) 
{ 
    this->size = size; 
} 

QString ResizeTransaction::message() 
{ 
    return QObject::tr("Resizing image..."); 
} 

QImage ResizeTransaction::apply(const QImage &image) 
{ 
    return image.scaled(size, Qt::IgnoreAspectRatio, 
         Qt::SmoothTransformation); 
} 

ConvertDepthTransaction::ConvertDepthTransaction(int depth) 
{ 
    this->depth = depth; 
} 

QImage ConvertDepthTransaction::apply(const QImage &image) 
{ 
    QImage::Format format; 

    switch (depth) { 
    case 1: 
     format = QImage::Format_Mono; 
     break; 
    case 8: 
     format = QImage::Format_Indexed8; 
     break; 
    case 24: 
    default: 
     format = QImage::Format_RGB32; 
    } 

    return image.convertToFormat(format); 
} 

QString ConvertDepthTransaction::message() 
{ 
    return QObject::tr("Converting image depth..."); 
} 

TransactionThread::TransactionThread() 
{ 
    start(); 
} 

TransactionThread::~TransactionThread() 
{ 
    { 
     QMutexLocker locker(&mutex); 
     while (!transactions.isEmpty()) 
      delete transactions.dequeue(); 
     transactions.enqueue(EndTransaction); 
     transactionAdded.wakeOne(); 
    } 

    wait(); 
} 

void TransactionThread::addTransaction(Transaction *transact) 
{ 
    QMutexLocker locker(&mutex); 
    transactions.enqueue(transact); 
    transactionAdded.wakeOne(); 
} 

void TransactionThread::run() 
{ 
    Transaction *transact = 0; 
    QImage oldImage; 

    forever { 
     { 
      QMutexLocker locker(&mutex); 

      if (transactions.isEmpty()) 
       transactionAdded.wait(&mutex); 
      transact = transactions.dequeue(); 
      if (transact == EndTransaction) 
       break; 

      oldImage = currentImage; 
     } 

     emit transactionStarted(transact->message()); 
     QImage newImage = transact->apply(oldImage); 
     delete transact; 

     { 
      QMutexLocker locker(&mutex); 
      currentImage = newImage; 
      if (transactions.isEmpty()) 
       emit allTransactionsDone(); 
     } 
    } 
} 

void TransactionThread::setImage(const QImage &image) 
{ 
    QMutexLocker locker(&mutex); 
    currentImage = image; 
} 

QImage TransactionThread::image() 
{ 
    QMutexLocker locker(&mutex); 
    return currentImage; 
} 
Смежные вопросы