1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include "widget.h" #include "ui_widget.h" #include<QPainter> #include<QtGui> #include<QDebug> #include<QFont>
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000); setWindowTitle(tr("我的时钟--designed by TW"));
}
Widget::~Widget() { delete ui; }
void Widget::paintEvent(QPaintEvent *event) { int side = qMin(width(), height()); QPainter painter(this); painter.fillRect(rect(),Qt::yellow); painter.setRenderHint(QPainter::Antialiasing); painter.translate(width() / 2, height() / 2); painter.scale(side / 200.0, side / 200.0);
QPen pen; pen.setWidth(2); pen.setStyle(Qt::SolidLine); pen.setColor(Qt::red);
painter.setPen(pen); for (int i = 0; i < 12; ++i) { painter.drawLine(87, 0, 95, 0); painter.rotate(30); }
pen.setWidth(1); pen.setColor(Qt::black); painter.setPen(pen);
for (int i = 0; i < 60; ++i) { if(i%5!=0) painter.drawLine(90, 0, 95, 0); painter.rotate(6); }
pen.setColor(Qt::black); painter.setPen(pen); QFont font("Century"); painter.setFont(font); painter.drawText(-6,-75,tr("12")); painter.drawText(-3,80,tr("6")); painter.drawText(75,5,tr("3")); painter.drawText(-80,5,tr("9"));
painter.drawArc(-3,-3,6,6,0,360*16); QTime time = QTime::currentTime();
painter.save(); painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); pen.setColor(Qt::green); pen.setWidth(4); painter.setPen(pen); painter.drawLine(0,0,0,-60); painter.restore(); painter.save(); painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); pen.setColor(Qt::blue); pen.setWidth(2); painter.setPen(pen); painter.drawLine(0,0,0,-80); painter.restore(); painter.save(); painter.rotate(6.0 *time.second() ); pen.setColor(Qt::red); pen.setWidth(1); painter.setPen(pen); painter.drawArc(-3,-66,6,6,0,360*16); painter.drawLine(0,20,0,-60); painter.drawLine(0,-66,0,-85); painter.restore();
}
|