#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tab->installEventFilter(this); //安装事件过滤器到窗口
ui->label->setText("current time zone:"+ui->comboBox->currentText());
QTime current_time = QTime::currentTime();
// 构造定时器,设置超时为 1 秒
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
ui->spinBox->setValue(current_time.hour());
ui->spinBox_2->setValue(current_time.minute());
ui->spinBox_3->setValue(current_time.second());
if(ui->spinBox->value()<12)
ui->radioButton->setChecked(true);
else
ui->radioButton_2->setChecked(true);
if(ui->spinBox->value()>12)
ui->spinBox->setValue(current_time.hour()-12);
}
MainWindow::~MainWindow()
{
//delete ui;
}
void MainWindow::on_pushButton_clicked()
{
apply();
}
void MainWindow::on_pushButton_2_clicked()
{
this->close();
}
void MainWindow::painter()
{
QPainter painter(ui->tab);
// 时针、分针、秒针位置 - 多边形
static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -30)
};
static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -65)
};
static const QPoint secondHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -80)
};
// 时针、分针、秒针颜色
QColor hourColor(176, 196, 222, 150);
QColor minuteColor(255, 182, 193, 200);
QColor secondColor(135, 206, 250, 150);
int side = qMin(width(), height());
QTime time = QTime::currentTime();
painter.setRenderHint(QPainter::Antialiasing);
// 平移坐标系原点至中心点
painter.translate(410, 130);
// 缩放
painter.scale(side / 550.0, side / 550.0);
// 绘制时针
painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);
painter.save();
// 每圈360° = 12h 即:旋转角度 = 小时数 * 30°
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 3);
painter.restore();
painter.setPen(hourColor);
// 绘制小时线 (360度 / 12 = 30度)
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();
// 每圈360° = 60m 即:旋转角度 = 分钟数 * 6°
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();
painter.setPen(minuteColor);
// 绘制分钟线 (360度 / 60 = 6度)
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
// 绘制秒针
painter.setPen(Qt::NoPen);
painter.setBrush(secondColor);
painter.save();
// 每圈360° = 60s 即:旋转角度 = 秒数 * 6°
painter.rotate(6.0 * time.second());
painter.drawConvexPolygon(secondHand, 3);
painter.restore();
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event){
if(watched == ui->tab && event->type() == QEvent::Paint){
painter();
return true;
}
else
return false;
return MainWindow::eventFilter(watched,event); //将事件传递给父类
}
void MainWindow::on_spinBox_valueChanged(int arg1)
{
if(ui->spinBox->value()==-1)
ui->spinBox->setValue(11);
if(ui->spinBox->value()==24)
ui->spinBox->setValue(0);
}
void MainWindow::on_spinBox_2_valueChanged(int arg1)
{
if(ui->spinBox_2->value()==60){
ui->spinBox->setValue(ui->spinBox->value()+1);
ui->spinBox_2->setValue(0);
}
if(ui->spinBox_2->value()==-1){
ui->spinBox->setValue(ui->spinBox->value()-1);
ui->spinBox_2->setValue(59);
}
}
void MainWindow::on_spinBox_3_valueChanged(int arg1)
{
if(ui->spinBox_3->value()==60){
ui->spinBox_2->setValue(ui->spinBox_2->value()+1);
ui->spinBox_3->setValue(0);
}
if(ui->spinBox_3->value()==-1){
ui->spinBox_2->setValue(ui->spinBox_2->value()-1);
ui->spinBox_3->setValue(59);
}
}
void MainWindow::apply()
{
QString str1,str2,str3,str4,str,tempStr;
str3 = ui->comboBox->currentText();
str1 = "*************************************";
str2 = "\nYear = "+QString::number(ui->calendarWidget->yearShown())+"\nMonth = "+QString::number(ui->calendarWidget->monthShown())
+"\nHour = "+ui->spinBox->value()+"\nMinute = "+ui->spinBox_2->value()+"\nSecond = "+ui->spinBox_3->value()
+"\nDay = "+ui->calendarWidget->selectedDate().toString("dd")+"\nTimezone = " + ui->comboBox->currentText()
+ "\nOffset from GMT = " + QString::number(timeZone(str3)) + " miniters\nAuto daylight = ";
if(judgeDayLight()) str4 = "True\n";
else str4 = "False\n";
str = str1 + str2 + str4 + str1;
QMessageBox::information(this, QString::fromUtf8("Date and Time"),"Time set to : \n"+str,QMessageBox::Yes);
}
int MainWindow::timeZone(QString str)
{
int s,m,n,l;
if(str[7] == '0') m = 0;
else if(str[7] == '1') m = 1;
else if(str[7] == '2') m = 2;
else if(str[7] == '3') m = 3;
else if(str[7] == '4') m = 4;
else if(str[7] == '5') m = 5;
else if(str[7] == '6') m = 6;
else if(str[7] == '7') m = 7;
else if(str[7] == '8') m = 8;
else if(str[7] == '9') m = 9;
if(str[9] == '0') n = 0;
else n = 3;
if(str[5] == '+')
{
s = 1;
if(str[6] == '0')
{
l = m*60 + n*10;
s = s * l;
}
else
{
l = m*60 + n*10 + 600;
s = s * l;
}
}
else{
s = -1;
if(str[6] == '0')
{
l = m*60 + n*10;
s = s * l;
}
else
{
l = m*60 + n*10 + 600;
s = s * l;
}
}
return s;
}
bool MainWindow::judgeDayLight()
{
QTime currentTime = QTime::currentTime();
int n = currentTime.hour()*60 + currentTime.minute() - 480;
n = timeZone(ui->comboBox->currentText()) - n;
if(n > -360 && n <= 360) return true;
else return false;
}
void MainWindow::on_pushButton_3_clicked()
{
apply();
this->close();
}
void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
ui->label->setText("current time zone:"+ui->comboBox->currentText());
}
qt做一个带有日历和钟表的界面
需积分: 50 159 浏览量
2018-11-26
22:58:52
上传
评论 3
收藏 13KB RAR 举报
暮月七
- 粉丝: 10
- 资源: 7
最新资源
- Video_1715568338521.mp4
- 大气红外探测仪AIRS-V5-Tropospheric-CO2-Products.pdf
- ECharts 数据可视化 -单仪表盘
- 135131858118956ssm某企业危化品信息管理系统bf339.zip
- ECharts 数据可视化 -基本雷达图
- 关于RFID RC522的驱动以及简单使用 stm32+RC522 spi2通信 通过串口1读取数据
- 一套适合互联网企业使用的开源支付系统,支持多渠道服务商和普通商户模式 已对接微信支付,支付宝,云闪付官方接口,支持聚合码支付
- Javaweb仓库管理系统项目源码.zip
- 文件整理,qt项目,c++变成
- 一氧化碳物性计算小软件.exe
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈