AnalogClock Class Reference

#include <analogclock.h>

Inheritance diagram for AnalogClock:

Inheritance graph
[legend]
Collaboration diagram for AnalogClock:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 30 of file analogclock.h.

Public Member Functions

 AnalogClock (QWidget *parent=0)
 AnalogClock (QWidget *parent=0)

Protected Member Functions

void paintEvent (QPaintEvent *event)
void paintEvent (QPaintEvent *event)


Constructor & Destructor Documentation

AnalogClock::AnalogClock ( QWidget parent = 0  ) 

Definition at line 28 of file analogclock.cpp.

References QObject::connect(), QWidget::resize(), QWidget::setWindowTitle(), SIGNAL, SLOT, QTimer::start(), and QWidget::update().

00029     : QWidget(parent)
00030 {
00031     QTimer *timer = new QTimer(this);
00032     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
00033     timer->start(1000);
00034 
00035     setWindowTitle(tr("Analog Clock"));
00036     resize(200, 200);
00037 }

Here is the call graph for this function:

AnalogClock::AnalogClock ( QWidget parent = 0  ) 


Member Function Documentation

void AnalogClock::paintEvent ( QPaintEvent event  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive paint events which are passed in the event parameter.

A paint event is a request to repaint all or part of the widget. It can happen as a result of repaint() or update(), or because the widget was obscured and has now been uncovered, or for many other reasons.

Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region(). This speed optimization does not change the result, as painting is clipped to that region during event processing. QListView and QTableView do this, for example.

Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). repaint() does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so that you're painting on the widget's background.

The background can be set using setBackgroundRole() and setPalette().

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker.

Note: Under X11 it is possible to toggle the global double buffering by calling qt_x11_set_global_double_buffer(). Example usage:

See also:
event(), repaint(), update(), QPainter, QPixmap, QPaintEvent, {Analog Clock Example}

Reimplemented from QWidget.

Definition at line 39 of file analogclock.cpp.

References QPainter::Antialiasing, QTime::currentTime(), QPainter::drawConvexPolygon(), QPainter::drawLine(), QWidget::height(), QTime::hour(), i, j, QTime::minute(), Qt::NoPen, qMin(), QPainter::restore(), QPainter::rotate(), QPainter::save(), QPainter::scale(), QTime::second(), QPainter::setBrush(), QPainter::setPen(), QPainter::setRenderHint(), QPainter::translate(), and QWidget::width().

00040 {
00041     static const QPoint hourHand[3] = {
00042         QPoint(7, 8),
00043         QPoint(-7, 8),
00044         QPoint(0, -40)
00045     };
00046     static const QPoint minuteHand[3] = {
00047         QPoint(7, 8),
00048         QPoint(-7, 8),
00049         QPoint(0, -70)
00050     };
00051 
00052     QColor hourColor(127, 0, 127);
00053     QColor minuteColor(0, 127, 127, 191);
00054 
00055     int side = qMin(width(), height());
00056     QTime time = QTime::currentTime();
00057 
00058     QPainter painter(this);
00059     painter.setRenderHint(QPainter::Antialiasing);
00060     painter.translate(width() / 2, height() / 2);
00061     painter.scale(side / 200.0, side / 200.0);
00062 
00063     painter.setPen(Qt::NoPen);
00064     painter.setBrush(hourColor);
00065 
00066     painter.save();
00067     painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
00068     painter.drawConvexPolygon(hourHand, 3);
00069     painter.restore();
00070 
00071     painter.setPen(hourColor);
00072 
00073     for (int i = 0; i < 12; ++i) {
00074         painter.drawLine(88, 0, 96, 0);
00075         painter.rotate(30.0);
00076     }
00077 
00078     painter.setPen(Qt::NoPen);
00079     painter.setBrush(minuteColor);
00080 
00081     painter.save();
00082     painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
00083     painter.drawConvexPolygon(minuteHand, 3);
00084     painter.restore();
00085 
00086     painter.setPen(minuteColor);
00087 
00088     for (int j = 0; j < 60; ++j) {
00089         if ((j % 5) != 0)
00090             painter.drawLine(92, 0, 96, 0);
00091         painter.rotate(6.0);
00092     }
00093 }

Here is the call graph for this function:

void AnalogClock::paintEvent ( QPaintEvent event  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive paint events which are passed in the event parameter.

A paint event is a request to repaint all or part of the widget. It can happen as a result of repaint() or update(), or because the widget was obscured and has now been uncovered, or for many other reasons.

Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region(). This speed optimization does not change the result, as painting is clipped to that region during event processing. QListView and QTableView do this, for example.

Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). repaint() does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so that you're painting on the widget's background.

The background can be set using setBackgroundRole() and setPalette().

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker.

Note: Under X11 it is possible to toggle the global double buffering by calling qt_x11_set_global_double_buffer(). Example usage:

See also:
event(), repaint(), update(), QPainter, QPixmap, QPaintEvent, {Analog Clock Example}

Reimplemented from QWidget.


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 14:43:42 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1