您的位置:首页 > 编程语言 > Qt开发

vs+qt 实现记录-设置时间

2019-07-01 10:45 1211 查看

需求:主窗体显示系统时间,用户可以通过子窗体修改软件时间,传参到主界面更改系统显示时间

用定时器显示系统时间

https://www.geek-share.com/detail/2702895306.html

QDateTime time = QDateTime::currentDateTime();

设定时间的控件QDateTimeEdit

https://blog.csdn.net/jia666666/article/details/81589442
https://blog.csdn.net/qq_40008325/article/details/88310432
https://www.geek-share.com/detail/2712421233.html
https://blog.csdn.net/liang19890820/article/details/52387275

修改软件时间

1.想法一(失败)
直接更改系统的时间
难点:获取系统的权限,时间更改的计算需要自己写
设置时间,使用SetSystemTime
https://www.geek-share.com/detail/2726162821.html
https://blog.csdn.net/qq_36809882/article/details/82621652
http://blog.sina.com.cn/s/blog_4c7fa77b01000at8.html
https://www.geek-share.com/detail/2693344820.html
https://bbs.csdn.net/topics/330236459提到了权限问题
https://bbs.csdn.net/topics/390023487

2.想法二(失败)
将设定时间与系统时间做差,用(差值+设定时间)显示
不是想要的效果没做下去
https://www.geek-share.com/detail/2727175417.html
https://blog.csdn.net/yangyang031213/article/details/80716215

3.想法三(成功)
自己写个定时器,触发事件是加一秒
http://www.kuqin.com/qtdocument/qdatetime.html QDateTime类中的addSecs(int nsecs) 加一秒
QTimer使用方法
https://www.geek-share.com/detail/2731992547.html
http://blog.sina.com.cn/s/blog_a6fb6cc90101drq0.html
http://www.360doc.com/content/13/1221/15/12424571_339023145.shtml
https://www.geek-share.com/detail/2727370398.html
http://blog.sina.com.cn/s/blog_bf5e23970102vtd2.html
总结:使用定时器可以成功实现,问题在于定时器随着父窗体释放,只能暂停,不能重置

//一个错误写法,改写法不能进行二次修改,由于静态设置的原因,第二次的时间设定参数传不进来
QTimer *timer = new QTimer;
timer->start(1000);
connect(timer, &QTimer::timeout, [=]() {
QDateTime current_time = ui.dateTimeEdit->dateTime();//获取控件的时间
static QDateTime current_time1 = current_time;
QDateTime current_time2 = current_time1.addSecs(1);//加一秒
current_time1 = current_time2;//静态1=0,进入定时器:2=1add=1  1=2  再次进入定时器
QString StrCurrentTime = current_time1.toString("yyyy-MM-dd hh:mm:ss ddd");//显示时间,格式为:年-月-日 时:分:秒 周几
ui.label_time->setText(StrCurrentTime);
});

再结合初始显示系统时间的需求

//成功实现
QTimer *timer = new QTimer;
timer->start(1000);
static QDateTime current_time1 = current_timew;
static QDateTime current_time2 = current_timew;
static QDateTime current_time3 = current_timew;
connect(timer, &QTimer::timeout, [=]() {
if (timeopen == 0)//如果没有时间设定的事件触发
{
current_time1 = QDateTime::currentDateTime();//系统时间
}
else //如果触发了时间选择
{
current_time2 = current_timew;
if (current_time2 != current_time3)//判断时间是否更改
{
current_time1 = current_timew;
current_time3 = current_timew;
}
current_time1 = current_time1.addSecs(1);
}
ui.label_time->setText(current_time1.toString("yyyy-MM-dd hh:mm:ss ddd"));
});

ps.我想用窗体进行传参(设定的时间),定时器是不能写在信号传递的参数的触发函数中的,还是要在主函数中实现,不然时间计算错误(2倍)
http://blog.csdn.net/qq78442761/article/details/80087409
全局

QDateTime current_timew;//全局,存储
int timeopen = 0;//全局,判断时间改变是否触发

信号

void HMI1::slot_time()
{
view_time = new TimeQtGuiClass;
view_time->setWindowModality(Qt::ApplicationModal); //设置父界面不可点击,子界面可以永远置顶
connect(view_time, SIGNAL(sendData(QDateTime)), this, SLOT(receiveData(QDateTime)));
view_time->show();
}
void HMI1::receiveData(QDateTime data)
{
timeopen = 1;//全局,判断时间改变是否触发
current_timew = data;//全局,存储
}

主函数

QTimer *timer3 = new QTimer;
timer3->start(1000);
static QDateTime current_time1 = current_timew;
static QDateTime current_time2 = current_timew;
static QDateTime current_time3 = current_timew;
connect(timer3, &QTimer::timeout, [=]() {
if (timeopen == 0)//如果没触发
{
current_time1 = QDateTime::currentDateTime();//系统时间
}
else //如果触发了时间选择
{
current_time2 = current_timew;
if (current_time2 != current_time3)//判断时间是否更改
{
current_time1 = current_timew;
current_time3 = current_timew;
}
current_time1 = current_time1.addSecs(1);
}
ui.label_time->setText(current_time1.toString("yyyy-MM-dd hh:mm:ss ddd"));
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: