⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.cpp

📁 程序代码使用说明: (1)所有源代码目录下都提供了Makefile(非Qt)
💻 CPP
字号:
#include "Clock.h"#include <QSettings>#include <QApplication>#include <QLabel>#include <QTimer>#include <QMessageBox>#include <ClockFace.h>#include <QVBoxLayout>#include <QHBoxLayout>#include <QTimeEdit>#include <QComboBox>#include <QPushButton>#include <QGroupBox>#include <QGridLayout>#include <QTime>#include <QDateTime>#include <time.h>#include <errno.h>Clock::Clock(QWidget *parent, Qt::WFlags f)    : QWidget(parent, f){    setWindowTitle("My Clock");    setGeometry(0, 20, 320, 220);    readSettings();    clockFace = new ClockFace;    clockFace->setGeometry(0, 0, 180, 180);        timeEditor = new QTimeEdit;    setEditorFormat();    formatCombo = new QComboBox;    formatCombo->addItem("12 Hour Clock", QVariant((int)CLOCK_12_HOUR));    formatCombo->addItem("24 Hour Clock", QVariant((int)CLOCK_24_HOUR));    setComboIndex();        QLabel *timeText = new QLabel("Change Time:");    QLabel *formatText = new QLabel("Change Time Format:");        QPushButton* quitBtn = new QPushButton("Quit");    QPushButton* saveBtn = new QPushButton("Save");        // layout widgets    QHBoxLayout *hlBtns = new QHBoxLayout;    hlBtns->addWidget(quitBtn);    hlBtns->addWidget(saveBtn);        QGroupBox *groupBox = new QGroupBox("Change Settings:");    QVBoxLayout *vlSettings = new QVBoxLayout;    vlSettings->addWidget(timeText);    vlSettings->addWidget(timeEditor);    vlSettings->addWidget(formatText);    vlSettings->addWidget(formatCombo);    groupBox->setLayout(vlSettings);    QVBoxLayout *vlPack = new QVBoxLayout;    vlPack->addWidget(groupBox);    vlPack->addStretch(1);    vlPack->addLayout(hlBtns);        QHBoxLayout *mainLayout = new QHBoxLayout;    mainLayout->addWidget(clockFace);    mainLayout->addLayout(vlPack);    mainLayout->setSpacing(20);    setLayout(mainLayout);    formatCombo->setFocus();    connect(quitBtn, SIGNAL(clicked()), qApp, SLOT(quit()));    connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSettings()));        connect(formatCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFormat(int)));    connect(timeEditor, SIGNAL(timeChanged(const QTime&)), this, SLOT(changeClock(const QTime&)));    QTimer::singleShot( 0, this, SLOT(updateClock()));        t = new QTimer(this);    connect(t, SIGNAL(timeout()), this, SLOT(updateClock()));    t->start(1000);}Clock::~Clock(){}void Clock::updateClock(){    if (timeEditor->hasFocus())        return;        QTime ct = QTime::currentTime();    clockFace->display(ct);    timeEditor->setTime(ct);}void Clock::changeClock(const QTime& time){    clockFace->display(time);}void Clock::readSettings(){    QSettings cfg("./MyClock.ini", QSettings::IniFormat);    timeFormat = (TimeFormat)(cfg.value("Time/Format", 0).toInt());}void Clock::setComboIndex(){    int index = formatCombo->findData(QVariant((int)timeFormat));    if (index > 0)    {        formatCombo->setCurrentIndex(index);    }}void Clock::saveSettings(){    // change the system time    QDateTime dt = QDateTime::currentDateTime();    dt.setTime(timeEditor->time());    time_t tt = (time_t)dt.toTime_t();    int r = stime(&tt);    if (r)    {        if (errno == EPERM)            QMessageBox::warning(this, "Error", "You don't have permission to change system time.");    }        // save time format    QSettings cfg("./MyClock.ini", QSettings::IniFormat);    cfg.setValue("Time/Format", QVariant((int)timeFormat));    qApp->quit();}void Clock::updateFormat(int index){    // get current selected time format    timeFormat = (TimeFormat)(formatCombo->itemData(index).toInt());        setEditorFormat();}void  Clock::setEditorFormat(){    QString formatStr = NULL;        switch (timeFormat)    {    case CLOCK_12_HOUR:        formatStr = "hh:mm AP";        break;    case CLOCK_24_HOUR:    default:        formatStr = "hh:mm";        break;    }    timeEditor->setDisplayFormat(formatStr);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -