本人使用Qt Creator,在XP系统下,最近纠结于如何修改显示系统时间 dialog.ui文件中,放置了time edit 和date edit两个控件,以及三个Label分别用于显示时间、日期和星期! 下面是代码部分:
dialog.h
C/C++ code?1
#ifndef DIALOG_H #define DIALOG_H
#include <QDialog> #include <QTimer> #include <QTime> #include <QDate> #include <windows.h>
namespace Ui { class Dialog; }
class Dialog : public QDialog { Q_OBJECT
public: explicit Dialog(QWidget *parent = 0); ~Dialog();
private: Ui::Dialog *ui;
private slots: bool timeedit(); bool dateedit(); void timedisplay(); };
#endif // DIALOG_H
dialog.cpp
C/C++ code?1
#include "dialog.h" #include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this);
connect(ui->timeEdit,SIGNAL(timeChanged(QTime)),this,SLOT(timeedit())); connect(ui->dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(dateedit()));
QTimer *timer = new QTimer; timer->start(1000); connect(timer,SIGNAL(timeout()),this,SLOT(timedisplay())); }
Dialog::~Dialog() { delete ui; }
void Dialog::timedisplay() { QTime ct = QTime::currentTime(); QDate cd = QDate::currentDate(); ui->timeLabel->setText(ct.toString("hh:mm")); ui->secondLabel->setText(ct.toString("s")); ui->dateLabel->setText(cd.toString("yyyy-MM-dd")); ui->weekLabel->setText(cd.toString("dddd")); }
bool Dialog::timeedit() { SYSTEMTIME st; GetSystemTime(&st); st.wHour=ui->timeEdit->time().hour(); st.wMinute=ui->timeEdit->time().minute(); return SetSystemTime(&st); }
bool Dialog::dateedit() { SYSTEMTIME st; GetSystemTime(&st); st.wYear = ui->dateEdit->date().year(); st.wMonth = ui->dateEdit->date().month(); st.wDay = ui->dateEdit->date().day(); return SetSystemTime(&st); } 问题来了!!!在time edit中修改时间,显示出来的系统时间与修改的时间有8小时的差,请教各位高手,到底是肿么了!?!?
时区,设置时区,东八区差8小时
|