Qt 타이머 QTimer

3996 단어 Qt 노트

Qt 타이머 QTimer


특정 클래스에서 타이머 기능을 사용할 경우 다음 두 가지 방법을 선택할 수 있습니다.


1. QObject에 상속된 경우 다음 함수를 직접 호출할 수 있습니다.
int QObject::startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer)

이 함수는 타이머를 켜지만 타이머의 번호만 되돌려줍니다. 타이머 대상을 가져올 수 없습니다. (타이머 대상을 만들었어도 QTimer 형식이 아니어야 합니다. 이것은 QObject 클래스의 구성원 함수이기 때문에 하위 클래스인 QTimer의 그림자가 나타나지 않아야 합니다.) 신호와 슬롯 메커니즘을 사용할 수 없습니다. 타이머 이벤트 처리 함수를 다시 불러올 수 있습니다.
[virtual protected] void QObject::timerEvent(QTimerEvent *event)

timerEvent () 함수에서 타이머 Id를 통해 여러 개의 타이머를 구분하고 각각 처리할 수 있습니다.
타이머를 끄면 클래스에서 다음 함수를 호출할 수 있습니다.
void QObject::killTimer(int id)

이 함수의 매개 변수는 타이머의 Id입니다.
여기서 Qt Assistant 의 example 을 참조하십시오.
  class MyObject : public QObject
  {
      Q_OBJECT

  public:
      MyObject(QObject *parent = 0);

  protected:
      void timerEvent(QTimerEvent *event);
  };

  MyObject::MyObject(QObject *parent)
      : QObject(parent)
  {
      startTimer(50);     // 50-millisecond timer
      startTimer(1000);   // 1-second timer
      startTimer(60000);  // 1-minute timer
  }

  void MyObject::timerEvent(QTimerEvent *event)
  {
      qDebug() << "Timer ID:" << event->timerId();
  }

2. 클래스에 QTimer 유형의 구성원을 추가할 수 있다. 그러면 우리는 신호와 슬롯 메커니즘 클래스를 통해 해당하는 기능을 실현할 수 있다. 예를 들어 다음과 같다.
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(update()));
      timer->start(1000);

QTimer의 슬롯 함수 start () 를 사용하여 타이머를 켜고 timerEvent 이벤트를 보내는 간격을 설정하거나 설정하지 않을 수 있습니다.
void start(int msec);
void start();

stop()을 사용하여 타이머를 중지하고 setInterval() 함수를 호출하는 두 번째 방법으로 간격을 설정합니다.
void setInterval(int msec);

setTimerType()을 호출하여 타이머의 정밀도를 설정합니다.
void setTimerType(Qt::TimerType atype);

상기 두 가지 방법은 두 가지 함수 모두Qt::TimerType 유형의 매개 변수를 사용했다. 다음에 상세하게 소개한다.


int QObject::startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);
void QTimer::setTimerType(Qt::TimerType atype);
Qt Assitant의 원문은 다음과 같습니다.
enum Qt::TimerType
The timer type indicates how accurate a timer can be.
Constant
Value
Description
Qt::PreciseTimer
0
Precise timers try to keep millisecond accuracy
Qt::CoarseTimer
1
Coarse timers try to keep accuracy within 5% of the desired interval
Qt::VeryCoarseTimer
2
Very coarse timers only keep full second accuracy
On UNIX (including Linux, macOS, and iOS), Qt will keep millisecond accuracy for Qt::PreciseTimer. For Qt::CoarseTimer, the interval will be adjusted up to 5% to align the timer with other timers that are expected to fire at or around the same time. The objective is to make most timers wake up at the same time, thereby reducing CPU wakeups and power consumption.
On Windows, Qt will use Windows's Multimedia timer facility (if available) for Qt::PreciseTimer and normal Windows timers for Qt::CoarseTimer and Qt::VeryCoarseTimer.
On all platforms, the interval for Qt::VeryCoarseTimer is rounded to the nearest full second (e.g. an interval of 23500ms will be rounded to 24000ms, and 20300ms to 20000ms).
이를 통해 알 수 있듯이 이 매개 변수는 타이머의 정밀도와 관련이 있다. 위에서 타이머를 사용하는 두 가지 방법 중 정밀도는 기본적으로 Qt::CoarseTimer이다. 나는 상술한 의미를 대체적으로 설명한다.
클래스 UNIX 시스템에서 Qt는 Qt::PreciseTimer를 사용하여 밀리초급 정밀도를 유지한다(정밀도 편차가 최대 1밀리초). Qt::CoarseTimer에 대해 실제 간격은 설정 간격보다 5% 많거나 적게 된다. 이 타이머는 같은 시간이나 가까운 시간에 동작을 하려고 하는 다른 타이머와 결합시킨다. 그러면 cpu는 한 번만 있으면 이 타이머를 깨워서 cpu의 깨우는 횟수와 소모량을 줄이고 소모량을 줄일 수 있다.Qt::Precise Timer를 사용할 때, 이 타이머 더미는 CPU로 각각 깨워야 할 수도 있고, 작업 소모도 증가할 수 있습니다.(본인의 졸견은 참고로 제공할 뿐, 진실로 믿지 마세요)
윈도우즈 플랫폼에서 Qt는 멀티미디어 타이머를 사용하여 Qt::PreciseTimer의 요구를 완성하고 일반적인 Windows 타이머를 사용하여 Qt:::Coarse와 Qt::VeryCoarseTimer에 대응합니다.
모든 플랫폼에서 Qt::verycoarseTimer 매개 변수는 인터페이스 매개 변수의 반올림을 정초의 형식으로 한다. 즉, 3위는 모두 0이다.

좋은 웹페이지 즐겨찾기