Recently wrote the first coding problem of QT4: the system time interface and the implementation of the modification of the system time.I summed up the resources on the Internet and made a summary here.
First of all, Qt itself does not provide a time setting function
Qt itself does not provide a time setting function, the implementation of the system to modify the time using the Windows native API function SetSystemTime Therefore, the header file needs to be added # include <windows.h>
Code section
1 2 3 4 5 6 7 8 9 10 |
//The following is part of the code of my program Void MainWindow::on_pushButton_3_clicked() { SYSTEMTIME st; GetLocalTime(&st); st.wHour=ui->spinBox->value(); st.wMinute=ui->spinBox_2->value(); st.wSecond=ui->spinBox_3->value(); SetLocalTime(&st); } |
common problem:
- Someone may find the following code online:
1 2 3 4 5 6 7 8 9 |
/ / Time zone issue with the following code Bool Dialog::timeedit() { SYSTEMTIME st; GetSystemTime(&st); st.wHour=ui->timeEdit->time().hour(); st.wMinute=ui->timeEdit->time().minute(); Return SetSystemTime(&st); } |
The result of running the above code shows that the time zone is incorrect: GetSystemTime() gets Greenwich time;
- No problem with the above, the program does not report an error, but the system time does not change
Please run QTcreator as administrator.
This article has been printed on copyright and is protected by copyright law. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click on the "sponsored author" below to reward the author!
Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/qt-how-to-modify-system-time-and-time-zone-error-problem-2.html
No Comment