MFC:OnTimer与QT:QTimer

一 MFC中定时器的使用

1.1、使用场景

定时读写数据,或者定时刷新界面,更新数据和状态。

1.2、使用

(1)编写OnTimer函数

void CMultipleCameraDlg::OnTimer(UINT_PTR nIDEvent)  //定时读取数据
{
    if(nIDEvent == WM_TIMER_GRAB_INFO)
    {
        int nRet = 0;
        CString pos;
        int nPos = 0;
        pos.Format(_T("%d"), nPos);

        unsigned int nLostFrame = 0;
        unsigned int nFrameCount = 0;
        MV_MATCH_INFO_NET_DETECT stMatchInfoNetDetect = {0};
        MV_MATCH_INFO_USB_DETECT stMatchInfoUSBDetect = {0};

        for (int i = 0; i < m_nSelectDeviceNum; i++)
        {
            MV_CC_DEVICE_INFO stDevInfo = {0};
            m_pcMyCamera[i]->GetDeviceInfo(&stDevInfo);

            if (stDevInfo.nTLayerType == MV_GIGE_DEVICE)
            {
                nRet = m_pcMyCamera[i]->GetGevAllMatchInfo(&stMatchInfoNetDetect);
                nLostFrame = stMatchInfoNetDetect.nLostFrameCount;
                nFrameCount = stMatchInfoNetDetect.nNetRecvFrameCount;
            }
            else if (stDevInfo.nTLayerType == MV_USB_DEVICE)
            {
                nRet = m_pcMyCamera[i]->GetU3VAllMatchInfo(&stMatchInfoUSBDetect);
                nLostFrame = stMatchInfoUSBDetect.nErrorFrameCount;
                nFrameCount = stMatchInfoUSBDetect.nReceivedFrameCount;
            }
            else
            {
                return;
            }

            if (MV_OK == nRet)
            {
                switch(i)
                {
                case 0:
                    {
                        m_nFrameCount1Edit = nFrameCount;
                        m_nLostFrame1Edit = nLostFrame;
                    }
                    break;
                case 1:
                    {
                        m_nFrameCount2Edit = nFrameCount;
                        m_nLostFrame2Edit = nLostFrame;
                    }
                    break;
                case 2:
                    {
                        m_nFrameCount3Edit = nFrameCount;
                        m_nLostFrame3Edit = nLostFrame;
                    }
                    break;
                case 3:
                    {
                        m_nFrameCount4Edit = nFrameCount;
                        m_nLostFrame4Edit = nLostFrame;
                    }
                    break;
                default:
                    break;
                }
                UpdateData(FALSE);   //界面数据和自定义变量进行同步
            }
        }
    }

    CDialog::OnTimer(nIDEvent);
}

(2)启动定时器

SetTimer(1,1000,NULL);      //参数:定时器标号,定时时间(ms)。启动定时器1,每隔1s刷新一次

(3)关闭定时器

 KillTimer(1);                               //关定时器1

二 QT中定时器使用

2.1 简述

QTimer类提供了重复和单次触发信号的定时器。

2.2 使用

(1)定义

QTimer *timer; // 定义的定时器

(2)使用

timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(function()));

// 调用自定义的槽函数
    timer->start(100);

(3)结束采集

timer->stop();

你可能感兴趣的:(MFC:OnTimer与QT:QTimer)