Return to Linux Life Edit

  ホーム · 全てのクラス · 主要なクラス · 注釈付き · グループ別 · 関数一覧

QTimerクラスリファレンス

QTimerクラスは繰り返し動作するシグナルとスロットによるタイマーを提供します。 詳細

#include <QTimer>

QtCore モジュールの一部です。

Inherits QObject.

Properties

Public関数

Publicスロット

シグナル

Static Publicメンバ

継承によるメンバ


詳しい説明

QTimerクラスは繰り返し動作するシグナルとスロットによるタイマーを提供します。

QTimerクラスはタイマーの為の高レベルプログラミングインターフェースを提供します。これを使用するには、QTimerを作成し、その timeout()シグナルを適切なスロットに接続し、 start()を呼び出してください。以後 timeout()シグナルが一定間隔で送信されます。

1秒(1000ミリ秒)タイマーの例( アナログ時計 の例より):

        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
        timer->start(1000);

これで、 update() スロットが毎秒呼び出されます。

setSingleShot(true)を呼び出すことによってタイマーを一度しかタイムアウトしないようにすることが出来ます。間隔を指定したあとで静的な QTimer::singleShot()を呼び出すことも可能です:

        QTimer::singleShot(200, this, SLOT(updateCaption()));

特別なケースとして、QTimerを0ミリ秒でタイムアウトさせるとウィンドウシステムのイベントキュー内の全てのイベントと同じくらいすぐにタイムアウトします。これは重い処理をすばやさの要求されるユーザインターフェースを提供しながら行う場合に使用できます。

        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
        timer->start();

processOneThing() は繰り返し呼び出されます。これはQtがイベントをウィジットに配送できるように常に素早く(通常は1つのデータアイテムを処理した後で)終了するように、また、処理が終了したら直ちにタイマーを停止するように書かれているべきです。これはGUIアプリケーション上で重い処理を行うための伝統的な方法ですが、今日多くのプラットフォームでマルチスレッディングがサポートされており、0秒QTimerによる方法は最終的に QThreadによって置き換えられると思われます。

QTimer の精度はオペレーティングシステムとハードウェアに依存することに注意してください。ほとんどのプラットフォームは1ミリ秒の精度をサポートしていますが、Windows95と98では55ミリ秒までしかサポートされていません。もし、Qtが要求された間隔でタイムアウトを搬送できない場合、それらのうちいくつかは暗黙のうちに破棄されます。

別のQTimerの使い方はあなたが用意したオブジェクトの QObject::startTimer()を呼び出して、 QObject::timerEvent()イベントハンドらをあなたが用意したクラスで再実装する方法です。(もちろんこのクラスは QObjectを継承しなければなりません)。この場合の問題点は timerEvent()はシングルショットやシグナルといった高度な機能をサポートしないことです。

さらに別のQTimerの使い方は QBasicTimerを使う方法です。これは基本的に QObject::startTimerを直接呼び出すほど厄介ではありません。これら3つのアプローチについては Timers を見てください。

いくつかのオペレーティングシステムは使用できるタイマーの数を制限するかもしれません。Qtはこの制限を迂回して動作することを試みます。

See also QBasicTimer, QTimerEvent, QObject::timerEvent(), and Timers.


Property Documentation

interval : int

This property holds the timeout interval in milliseconds.

The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.

Setting the interval of an active timer changes its timerId().

Access functions:

See also singleShot.

singleShot : bool

This property holds whether the timer is a single-shot timer.

A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.

Access functions:

See also interval and singleShot().


Member Function Documentation

QTimer::QTimer ( QObject * parent = 0 )

タイマーの値を与えられた parentにして初期化します。

QTimer::~QTimer ()

タイマーを破棄します。

bool QTimer::isActive () const

もしタイマーが稼動中ならtrueを、そうでなければfalseを返します。

void QTimer::singleShot ( int msec, QObject * receiver, const char * member )   [static]

この静的関数は設定された時間経過後にスロットを呼び出します。

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

Example:

    #include <QApplication>
    #include <QTimer>
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QTimer::singleShot(60000, &app, SLOT(quit()));
        ...
        return app.exec();
    }

This sample program automatically terminates after 10 minutes (600000 milliseconds).

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.

Note: This function is reentrant.

See also start().

void QTimer::start ( int msec )   [slot]

Starts or restarts the timer with a timeout interval of msec milliseconds.

void QTimer::start ()   [slot]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Starts or restarts the timer with the timeout specified in interval.

If singleShot is true, the timer will be activated only once.

void QTimer::stop ()   [slot]

Stops the timer.

See also start().

void QTimer::timeout ()   [signal]

This signal is emitted when the timer times out.

See also interval, start(), and stop().

int QTimer::timerId () const

Returns the ID of the timer if the timer is running; otherwise returns -1.


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0