標籤:blank alt 訊號 init body bsp src 解決辦法 obj
一、本文
目前正在做一個視頻處理相關的項目。項目的技術棧是這樣的,UI層採用Qt來實現基本的資料展示和互動,底層音視頻採用的是一套基於FFmpeg的視頻處理架構。這是一套類似Microsoft Media Foundation的處理架構,採用管道流進行架構,解複用、解碼、複用、編碼及使用者自訂動作都採用Filter組件來實現,靈活度和可擴充性都比較好。(基本上常用音視頻處理架構都採用了這一架構,如Microsoft Media Foundation, DirectShow Filter, gstreamer)
項目要求視頻在處理的過程中,即時計算出當前的進度並展示在UI上,方便使用者隨時查看處理進度。想象中的處理方式是:負責Progress的Filter每一次計算進度的時候都發送一個訊號給UI,方便更新進度條。於是ProgressFilter的實現大致如下:
template<typename DataType>class ProgressFilter : public ins::MediaFilter<DataType,DataType>, public QObject{Q_OBJECTpublic: ~ProgressFilter() = default;ProgressFilter(uint64_t totalFrames = 1) :mCurrentFrameIndex(0),mTotalFrames(totalFrames) {}bool Init(ins::MediaContext* ctx) {return this->next_filter_->Init(ctx);}bool Filter(ins::MediaContext* ctx, const DataType& data) { double progress = std::min((double)(++mCurrentFrameIndex) / mTotalFrames, 0.99);emit progressChanged(progress);return this->next_filter_->Filter(ctx, data);}void Close(ins::MediaContext* ctx) {this->next_filter_->Close(ctx);}void Notify(ins::MediaContext* ctx, const ins::MediaNotify& notify) {if (notify.type == ins::kNotifyEOF) {double progress = std::min(std::ceil((double)mCurrentFrameIndex / mTotalFrames), 1.0);emit progressChanged(progress);}this->next_filter_->Notify(ctx, notify);}signals:void progressChanged(double progress);private:uint64_t mCurrentFrameIndex;uint64_t mTotalFrames;};
然而編譯的時候卻提示報錯:
錯誤提示已經很顯然了,Q_OBJECT宏不支援C++模板類。Qt中一個類如果需要支援訊號槽機制,那麼必須要加一個Q_OBJECT做預先處理。而項目當前使用的這套視頻處理架構又大量使用了模板技術,改源碼顯然不大現實。那麼就沒有辦法了嗎?網上搜了一下就找到了一個解決辦法。那就是實現一個普通的中間類,在這個類中定義訊號槽。然後再讓模板類繼承這個中間類即可。所以我們實現一個中間類:
class Proxy : public QObject {Q_OBJECTpublic:explicit Proxy(QObject *parent = 0) : QObject(parent) {} signals:void progressChanged(double progress);};template<typename DataType>class ProgressFilter : public ins::MediaFilter<DataType,DataType>, public Proxy { public: ~ProgressFilter() = default; ......}
這樣,我們的模板類就可以正常使用訊號槽機制了。
二、參考連結
1. https://stackoverflow.com/questions/4397478/qt-templated-q-object-class
2. https://stackoverflow.com/questions/4238204/is-it-possible-to-mix-template-derived-c-classes-with-qts-q-object
C++模板類與Qt訊號槽混用