time and painter

Topik sebelumnya Topik selanjutnya Go down

time and painter

Post  cpp on Mon Dec 07, 2009 5:14 pm

analogclock.h
Code:

#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H

#include <QWidget>
class AnalogClock : public QWidget
{
    Q_OBJECT

public:
    AnalogClock(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *event);
};
//! [0]

#endif
analogclock.cpp
Code:

#include <QtGui>
#include "analogclock.h"
AnalogClock::AnalogClock(QWidget *parent)
    : QWidget(parent)
{
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(1000);
    resize(200, 200);
}
void AnalogClock::paintEvent(QPaintEvent *event)
{
    /*
      fungsi ini akan selalu dipanggil/dijalankan
      setiap detik.
    */
    //membuat jam
    QTime time = QTime::currentTime();
    QString text = time.toString("hh:mm");
    if ((time.second() % 2) == 0)
        text[2] = ' ';
    //menyimpan lebar dan tinggi
    int h = height();
    int w  = width();
    //mulai mewarnai
    QPainter painter(this);

    QFont font("Monospace", w/4, QFont::DemiBold);
    painter.setPen(QPen(QBrush("#575555"), 3));
    font.setItalic(false);
    painter.setFont(font);
    painter.drawText(rect(), Qt::AlignCenter,text);
}
main.cpp
Code:

#include <QApplication>

#include "analogclock.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    AnalogClock clock;
    clock.show();
    return app.exec();
}

cpp
Pejuang
Pejuang

Jumlah posting : 35
Registration date : 15.03.09

Lihat profil user

Kembali Ke Atas Go down

Topik sebelumnya Topik selanjutnya Kembali Ke Atas


 
Permissions in this forum:
Anda tidak dapat menjawab topik