Qt reads and sets the system time in a Linux environment
This blog link: http://blog.csdn.NET/jdh99, author: jdh, reprint please specify.
Environment :
Host: Fedora12
Development software: QT
Read system time
[CPP]View PlainCopy
- void Moreiddialog::refresh_time ()
- {
- Qdatetime time;
- Txt_time->settext (Time.currentdatetime (). toString ("yyyy") + "." + \
- Time.currentdatetime (). ToString ("M") + "." + \
- Time.currentdatetime (). toString ("D") + "." + \
- Time.currentdatetime (). ToString ("h") + "." + \
- Time.currentdatetime (). toString ("M"));
- }
Read to minute, time interval with ".", finally read and show the effect: 2011.12.27.9.14
Setting the system time
[CPP]View PlainCopy
- Save Time Key
- void Moreiddialog::slot_save_time_key ()
- {
- Qdatetime time;
- QString str = Txt_time->text ();
- //Determine if the format is correct
- if (Str.count (".")! = 4)
- {
- Txt_time->settext (tr ("ge Shi CuO wu"));
- return;
- }
- int i = 0,j = 0;
- i = Str.indexof (".");
- QString year = Str.mid (0,i);
- j = Str.indexof (".", i + 1);
- QString month = Str.mid (i + 1,j-i-1);
- i = j;
- j = Str.indexof (".", i + 1);
- QString Day = Str.mid (i + 1,j-i-1);
- i = j;
- j = Str.indexof (".", i + 1);
- QString hour = Str.mid (i + 1,j-i-1);
- i = j;
- j = Str.indexof (".", i + 1);
- QString min = str.mid (i + 1,j-i-1);
- bool OK = false;
- Year.toint (&ok);
- if (ok = = false)
- {
- Txt_time->settext (tr ("ge Shi CuO wu"));
- return;
- }
- Month.toint (&ok);
- if (ok = = false)
- {
- Txt_time->settext (tr ("ge Shi CuO wu"));
- return;
- }
- Day.toint (&ok);
- if (ok = = false)
- {
- Txt_time->settext (tr ("ge Shi CuO wu"));
- return;
- }
- Hour.toint (&ok);
- if (ok = = false)
- {
- Txt_time->settext (tr ("ge Shi CuO wu"));
- return;
- }
- Min.toint (&ok);
- if (ok = = false)
- {
- Txt_time->settext (tr ("ge Shi CuO wu"));
- return;
- }
- str = "date-s" + month + "/" + Day + "/" + year;
- System (Str.tolatin1 (). data ());
- str = "date-s" + Hour + ":" + min + ":" + "00";
- System (Str.tolatin1 (). data ());
- //Force write to CMOS
- System ("clock-w");
- }
Synchronize the system clock with the hardware clock time command:Hardware clock synchronization to system clock: Hwclock--hctosys system clock sync to hardware clock: HWCLOCK-SYSTOHC
http://blog.csdn.net/jdh99/article/details/7102196
Qt reads and sets the system time in a Linux environment (directly calling Linux commands via system, paying attention to permissions issues)