Qt样例学习1(数字时钟)

/*main.cpp*/
#include <QApplication>
#include <QTextCodec>
#include "digitalclock.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    //显示本地化,解决中文乱码.
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    DigitalClock clock;
    clock.show();
    return app.exec();
}

#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H

#include <QLCDNumber>

class DigitalClock : public QLCDNumber
{
    Q_OBJECT

public:
    DigitalClock(QWidget *parent = 0);

private slots:
    void showTime();
};

#endif

#include <QtGui>

#include "digitalclock.h"

DigitalClock::DigitalClock(QWidget *parent)
    : QLCDNumber(parent)
{
    //setSegmentStyle(Filled);
    //setSegmentStyle(Outline);
    setNumDigits(8);

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

    showTime();

    //设置窗口标题
    setWindowTitle(tr("数字时钟"));
    resize(200, 60);
}

void DigitalClock::showTime()
{
    QTime time = QTime::currentTime();
    QString text = time.toString("hh:mm:ss");
    if ((time.second() % 2) == 0)
        text[2] = ' ';
    display(text);
}


你可能感兴趣的:(qt,qt,qt)