Qt+halcon 第二个程序 定时器类QTimer的应用

5 篇文章 0 订阅
以下内容来源于帮助说明
The QTimer class provides repetitive and single-shot timers.
The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on, it will emit the timeout() signal at constant intervals.
Example for a one second (1000 millisecond) timer (from the Analog Clock example):
      QTimer *timer = new QTimer(this);                                         //创建一个QTimer对象;
      connect(timer, SIGNAL(timeout()), this, SLOT(update()));     // 将该对象的信号 timeout() 链接到槽函数  update()
      timer->start(1000);                                                                 //启动timer。
From then on, the update() slot is called every second.

You can set a timer to time out only once by calling setSingleShot(true).You can also use the static QTimer::singleShot() function to call a slot after a specified interval:
      QTimer::singleShot(200, this, SLOT(updateCaption()));

In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec(). Qt uses the timer's thread affinity to determine which thread will emit the timeout() signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.

As a special case, a QTimer with a timeout of 0 will time out as soon as all the events in the window system's event queue have been processed. This can be used to do heavy workwhile providing a snappy user interface:
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
      timer->start();
From then on, processOneThing() will be called repeatedly. It should be written in such a way that it always returns quickly (typically after processing one data item) so that Qt can deliver events to the user interface and stop the timer as soon as it has done all its work. This is the traditional way of implementing heavy work in GUI applications, but as multithreading is nowadays becoming available on more and more platforms, we expect that zero-millisecond QTimer objects will gradually be replaced by QThreads.
Accuracy and Timer Resolution
The accuracy of timers depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.
The accuracy also depends on the timer type. For Qt::PreciseTimer, QTimer will try to keep the accurance at 1 millisecond. Precise timers will also never time out earlier than expected.
For Qt::CoarseTimer and Qt::VeryCoarseTimer types, QTimer may wake up earlier than expected, within the margins for those types: 5% of the interval for Qt::CoarseTimer and 500 ms for Qt::VeryCoarseTimer.
All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy. In such a case of timeout overrun, Qt will emit activated() only once, even if multiple timeouts have expired, and then will resume the original interval.

前面的程序用QTimer类后
widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>

#include "HalconCpp.h"
using namespace HalconCpp;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    void resizeEvent(QResizeEvent*);    //从写resize事件
    
private:
    Ui::Widget *ui;

private slots:
    void imgGrabStartOrStop(bool flag);
    void imgShow();

public:
    HObject ho_Image;
    HTuple  hv_AcqHandle;
    HTuple hv_Width, hv_Height, hv_WindosID;
    Hlong winID;
    QTimer *imgShowTimer;
};

#endif // WIDGET_H

widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QTimer>

#define fpsOfCamera 35

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    winID = (Hlong)ui->imageShowLabel->winId();
    SetCheck("~father");
    OpenWindow(0,0,(Hlong)ui->imageShowLabel->width(),(Hlong)ui->imageShowLabel->height(),winID,"","",&hv_WindosID);
    SetCheck("father");
    imgShowTimer = new QTimer(this);
    connect(imgShowTimer,SIGNAL(timeout()),this,SLOT(imgShow()));
}

Widget::~Widget()
{
    delete ui;
}
void Widget::imgGrabStartOrStop(bool flag)
{
    if(flag)
    {
        qDebug()<<"Started grabing";
        OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false",
            "default", "[0] Lenovo EasyCamera", 0, -1, &hv_AcqHandle);
        GrabImage(&ho_Image, hv_AcqHandle);
        GetImageSize(ho_Image,&hv_Width,&hv_Height);
        SetPart(hv_WindosID,0,0,hv_Height-1,hv_Width-1);
        imgShowTimer->start(1000/fpsOfCamera);
        qDebug()<<"imgShowTimerId:"<<imgShowTimer->timerId();
    }
    else
    {
        qDebug()<<"stoped grabing! imgShowTimerId:"<<imgShowTimer->timerId();
        imgShowTimer->stop();
        CloseFramegrabber(hv_AcqHandle);
    }
}

void Widget::imgShow()
{
    GrabImage(&ho_Image, hv_AcqHandle);
    DispObj(ho_Image,hv_WindosID);
}
//resizeEvent用来使窗口发生改变时,显示图形大小也来发生变化。ui设计时要选择栅格布局
void Widget::resizeEvent(QResizeEvent *)
{
    SetWindowExtents(hv_WindosID,0,0,(Hlong)ui->imageShowLabel->width(),(Hlong)ui->imageShowLabel->height());
    qDebug()<<"winID:"<<winID;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值