簡體   English   中英

Qt Gui 線程阻塞問題

[英]Qt Gui Thread is blocking Issue

我是初級程序員

最近,我使用 Halcon 庫實現了圖像的抓取。

當我按下實時按鈕時,定時器開始抓取圖像。 它可以工作,但主屏幕凍結到計時器周期。

所以,我正在提高使用線程抓取圖像的性能

首先我實現了這樣的線程

[ImageUpdateWorker.h]

class ImageUpdateWorker : public QObject
{
    Q_OBJECT

public:
    explicit ImageUpdateWorker(QObject* parent = 0, QString strThreadName = "ImageUpdateWorker");
    ~ImageUpdateWorker();

signals:
    void finished();
    void grab();

public slots:
    void run();

private:
    bool m_bStop{ false };
};

[ImageUpdateWorker.cpp]

ImageUpdateWorker::ImageUpdateWorker(QObject* parent, QString strThreadName)
    : QObject(parent)
{
    setObjectName(strThreadName);
}

ImageUpdateWorker::~ImageUpdateWorker()
{
}

void ImageUpdateWorker::run()
{
    while (m_bStop == false)
    {
        emit grab();
    }

    emit finished();
}

第二個我實現了繼承的 QWidget UI 小部件,輸出屏幕是這樣的

    m_pThread = new QThread();
    m_pUpdateWorker = new ImageUpdateWorker(nullptr, strName);
    m_pUpdateWorker->moveToThread(m_pThread); // UpdateWorker move to Thread

    connect(m_pThread, SIGNAL(started()), m_pUpdateWorker, SLOT(run()));
    connect(m_pThread, SIGNAL(finished()), m_pThread, SLOT(deleteLater()));
    connect(m_pUpdateWorker, SIGNAL(finished()), m_pThread, SLOT(quit()));
    connect(m_pUpdateWorker, SIGNAL(finished()), m_pUpdateWorker, SLOT(deleteLater()));
    connect(m_pUpdateWorker, SIGNAL(grab()), this, SLOT(onGrab()));

當我調用“m_pThread->start();” 屏幕開始阻塞 :(

如果您有任何建議或信息,我將不勝感激。 謝謝你的閱讀。

我不知道在 QT 中。 我向您發送了我在 C# 中使用的代碼。

如果您不想凍結 GUI,主要是您必須使用委托。

hdisplay 是對象 HalconDotNet:HWindowControlWPF。

相機是我定義相機參數的類。

在camera.Grab里面有代碼:

  HOperatorSet.GrabImage(out ho_Image, _AcqHandle);
  h_Image = new HImage(ho_Image);

在初始化有代碼:

    // Initialise the delegate 
    updateLiveDelegate = new UpdateLiveDelegate(this.UpdateLive);

    HImage ho_Image = new HImage();

這里我使用的代碼:

    // ==================
    // LIVE
    // ==================

    bool stopLive = true;

    // Declare the thread
    private Thread liveThread = null;

    // Declare a delegate used to communicate with the UI thread
    private delegate void UpdateLiveDelegate();
    private UpdateLiveDelegate updateLiveDelegate = null;


    private void btnLive_Click(object sender, RoutedEventArgs e)
    {

        try
        {
            stopLive = !stopLive;

            // if stopLive = false, live camera is activated
            if (!stopLive)
            {

                // Launch the thread
                liveThread = new Thread(new ThreadStart(Live));
                liveThread.Start();
            }


        }
        catch (Exception ex)
        {
            // Error

        }

    }


    private void Live()
    {

        try
        {

            while (stopLive == false)
            {

                if (camera.Grab(out ho_Image))
                {
                    // Show progress
                    Dispatcher.Invoke(this.updateLiveDelegate);
                }
                else
                {
                    // No grab
                    stopLive = true;

                }
            }

            // here stopLive is true


        }
        catch (Exception ex)
        {
            // Error
        }




    }

    private void UpdateLive()
    {

        try
        {
            int imageHeight;
            int imageWidth;
            string imageType;
            ho_Image.GetImagePointer1(out imageType, out imageWidth, out imageHeight);

            hDisplay.HalconWindow.SetPart(0, 0, imageHeight - 1, imageWidth - 1);

            // display
            hDisplay.HalconWindow.DispImage(ho_Image);
        }
        catch (Exception ex)
        {
            // Error
        }


    }

使用 m_pImageUpdateThread->moveToThread(m_pThread);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM