Qt+halcon 第二个程序 定时器类QTimer的应用
2017-03-01 14:29
323 查看
以下内容来源于帮助说明
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
widget.cpp
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; }
相关文章推荐
- Qt+halcon 第一个程序 QObject类的定时器应用
- Qt模块化笔记之network——第二个TCP程序,传输图片文件的实现(1)
- Qt开发程序在Windows 10应用需要管理员运行的解决思路
- QT 第二个程序
- 第二个Struts2程序 应用动态Action
- Qt开发程序在Windows 10应用须要管理员执行的解决思路
- qt程序应用记录
- Qt模块化笔记之network——第二个TCP程序,传输图片文件的实现(2)
- QTcpServer、QTcpSocket、QUdpSocket在聊天程序上的应用
- Android应用开发揭秘的第二个程序3_1_Android_Intent修改版和高仿版的源码注释
- Qt模块化笔记之network——第二个TCP程序,传输图片文件的实现(2)
- 应用笔记-Qt 程序中使用 Live 20R 指纹采集器
- 存取程序状态的几种方法——Java I/O应用杂谈
- 在.NET客户端程序中应用多线程
- 使用Installshield制作asp,asp.net应用的安装程序
- bcb做三层数据库程序的应用服务器接口
- 如何正确地应用Runtime类调用程序
- 程序优化杂谈——最优二叉树在程序中的应用
- 应用Spring开发彩信程序
- 快速开发SMS应用的程序接口