Qt Gui线程正在阻止问题

问题描述

我是初级程序员

最近,我已经使用Halcon库实现了Image的抓取。

当我按下实时按钮时,计时器开始抓取图像。它可以工作,但是主屏幕冻结到计时器周期。

所以,我正在提高使用Thread捕获图像的性能

首先,我实现了这样的线程

[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 Widget,其输出屏幕是这样的

    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,SLOT(quit()));
    connect(m_pUpdateWorker,SIGNAL(grab()),this,SLOT(onGrab()));

当我调用“ m_pThread-> start();”时屏幕开始暗淡:(

如果您有任何建议或信息,我们将不胜感激。谢谢您的阅读。

解决方法

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

主要是,如果您不想冻结GUI,则必须使用委托。

hdisplay是对象HalconDotNet:HWindowControlWPF。

camera是我定义相机参数的类。

内置摄像头。抓取代码:

  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,imageHeight - 1,imageWidth - 1);

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


    }
,

使用m_pImageUpdateThread-> moveToThread(m_pThread);