vs+qt 实现记录-设置时间
需求:主窗体显示系统时间,用户可以通过子窗体修改软件时间,传参到主界面更改系统显示时间
用定时器显示系统时间
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")); });
- 一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
- VS中QT的智能提示设置
- Linux下QT实现自定义对话框以及设置模态和非模态对话框
- Linux及QT时间设置
- Time 类可以实现调整和重新设置,显示时间,将两个时间相加的功能
- php中实现精确设置session过期时间的方法
- 工作记录--WPF自定义控件,实现一个可设置编辑模式的TextBox
- 使用Visual Studio(VS)开发Qt程序代码提示功能的实现
- Qt开发的应用记录读取用户习惯设置的方法
- vs+qt exe设置图标
- Qt 不规则窗体的实现(构造函数里setPaletteBackgroundPixmap后设置setMask)
- 基于VS C++平台的OpenCV设置,实现简单的行人检测
- 5.11 一个用Spring AOP实现异常处理和记录程序执行时间的实例
- php中实现精确设置session过期时间的方法
- QT工程pro设置实践(with QtCreator)----非弄的像VS一样才顺手?
- QT下设置Linux系统时间
- vs下qt工程设置exe图标
- Qt实现屏保功能,鼠标一定时间无响应程序进入待机状态
- Qt中DateTimeEdit等时间日期控件设置显示格式
- mysql更新记录时设置自动更新时间戳