#include <QtGui>
#include "analogclock.h"
#include <qdebug>
int segundo=0;
static const QPoint hourHand
[3] = { };
static const QPoint minuteHand
[3] = { };
static const QPoint secondHand
[3] = { };
static const QPoint centesima
[3] = { };
QColor hourColor
(127,
0,
127);
QColor minuteColor
(0,
127,
127,
191);
QColor secondColor
(255,
127,
127,
191);
QColor centeColor
(255,
127,
127,
191);
void laimagen(int x, int y)
{
image
= QImage(x, y,
QImage::Format_ARGB32_Premultiplied);
//QImage::Format_RGB32
}
{
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
setWindowTitle(tr("Analog Clock"));
resize(200, 200);
segundo=0;
laimagen(200,200);
timer->start(10);
}
{
if (segundo==0) segundo=thetime.second();
if (thetime.second()==segundo )
{
QPainter thepainter
(&image
);
// se pinta en la imagen dibuja( &thepainter, thetime);
segundo=(++segundo==60)?0:segundo++;
}
else
{
thepainter.drawImage(image.rect(), image, image.rect());
}
}
int wi=event->size().width();
int he=event->size().height();
laimagen(wi,he);
}
int side = qMin(width(), height());
painter
->setRenderHint
(QPainter::Antialiasing);
painter->translate(width() / 2, height() / 2);
painter->scale(side / 200.0, side / 200.0);
painter->setPen(Qt::NoPen);
painter->setBrush(hourColor);
painter->save();
painter->rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter->drawConvexPolygon(hourHand, 3);
painter->restore();
painter->setPen(hourColor);
for (int i = 0; i < 12; ++i) {
painter->drawLine(88, 0, 96, 0);
painter->rotate(30.0);
}
painter->setPen(Qt::NoPen);
painter->setBrush(minuteColor);
painter->save();
painter->rotate(6.0 * (time.minute() + time.second() / 60.0));
painter->drawConvexPolygon(minuteHand, 3);
painter->restore();
painter->save();
painter->rotate(time.second()*6.0);
painter->drawConvexPolygon(secondHand, 3);
painter->restore();
painter->save();
painter->setPen(minuteColor);
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter->drawLine(92, 0, 96, 0);
painter->rotate(6.0);
}
painter->restore();
painter->save();
painter->setPen(centeColor);
for (int j = 0; j < 360; ++j) {
painter->drawLine(90, 0, 92, 0);
painter->rotate(1.0);
}
painter->restore();
}
#include <QtGui>
#include "analogclock.h"
#include <qdebug>
int segundo=0;
QImage image;
static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -60)
};
static const QPoint secondHand[3] = {
QPoint(3, 12),
QPoint(-3, 12),
QPoint(0, -70)
};
static const QPoint centesima[3] = {
QPoint(2, 12),
QPoint(-2, 12),
QPoint(0, -80)
};
QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);
QColor secondColor(255, 127, 127, 191);
QColor centeColor(255, 127, 127, 191);
void laimagen(int x, int y)
{
image= QImage(x, y, QImage::Format_ARGB32_Premultiplied); //QImage::Format_RGB32
}
AnalogClock::AnalogClock(QWidget *parent) : QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
setWindowTitle(tr("Analog Clock"));
resize(200, 200);
segundo=0;
laimagen(200,200);
timer->start(10);
}
void AnalogClock::paintEvent(QPaintEvent *)
{
QTime thetime = QTime::currentTime();
if (segundo==0) segundo=thetime.second();
if (thetime.second()==segundo )
{
QPainter thepainter(&image); // se pinta en la imagen
dibuja( &thepainter, thetime);
segundo=(++segundo==60)?0:segundo++;
}
else
{
QPainter thepainter(this);
thepainter.drawImage(image.rect(), image, image.rect());
}
}
void AnalogClock::resizeEvent(QResizeEvent *event) {
int wi=event->size().width();
int he=event->size().height();
laimagen(wi,he);
}
void AnalogClock::dibuja(QPainter *painter, QTime time) {
int side = qMin(width(), height());
painter->setRenderHint(QPainter::Antialiasing);
painter->translate(width() / 2, height() / 2);
painter->scale(side / 200.0, side / 200.0);
painter->setPen(Qt::NoPen);
painter->setBrush(hourColor);
painter->save();
painter->rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter->drawConvexPolygon(hourHand, 3);
painter->restore();
painter->setPen(hourColor);
for (int i = 0; i < 12; ++i) {
painter->drawLine(88, 0, 96, 0);
painter->rotate(30.0);
}
painter->setPen(Qt::NoPen);
painter->setBrush(minuteColor);
painter->save();
painter->rotate(6.0 * (time.minute() + time.second() / 60.0));
painter->drawConvexPolygon(minuteHand, 3);
painter->restore();
painter->save();
painter->rotate(time.second()*6.0);
painter->drawConvexPolygon(secondHand, 3);
painter->restore();
painter->save();
painter->setPen(minuteColor);
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter->drawLine(92, 0, 96, 0);
painter->rotate(6.0);
}
painter->restore();
painter->save();
painter->setPen(centeColor);
for (int j = 0; j < 360; ++j) {
painter->drawLine(90, 0, 92, 0);
painter->rotate(1.0);
}
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks