Qt reads and sets the system time in a Linux environment (directly calls the Linux command via system, paying attention to permissions issues)

Source: Internet
Author: User

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
  1. void Moreiddialog::refresh_time ()
  2. {
  3. Qdatetime time;
  4. Txt_time->settext (Time.currentdatetime (). toString ("yyyy") + "." + \
  5. Time.currentdatetime (). ToString ("M") + "." + \
  6. Time.currentdatetime (). toString ("D") + "." + \
  7. Time.currentdatetime (). ToString ("h") + "." + \
  8. Time.currentdatetime (). toString ("M"));
  9. }
Read to minute, time interval with ".", finally read and show the effect: 2011.12.27.9.14 Setting the system time [CPP]View PlainCopy
  1. Save Time Key
  2. void Moreiddialog::slot_save_time_key ()
  3. {
  4. Qdatetime time;
  5. QString str = Txt_time->text ();
  6. //Determine if the format is correct
  7. if (Str.count (".")! = 4)
  8. {
  9. Txt_time->settext (tr ("ge Shi CuO wu"));
  10. return;
  11. }
  12. int i = 0,j = 0;
  13. i = Str.indexof (".");
  14. QString year = Str.mid (0,i);
  15. j = Str.indexof (".", i + 1);
  16. QString month = Str.mid (i + 1,j-i-1);
  17. i = j;
  18. j = Str.indexof (".", i + 1);
  19. QString Day = Str.mid (i + 1,j-i-1);
  20. i = j;
  21. j = Str.indexof (".", i + 1);
  22. QString hour = Str.mid (i + 1,j-i-1);
  23. i = j;
  24. j = Str.indexof (".", i + 1);
  25. QString min = str.mid (i + 1,j-i-1);
  26. bool OK = false;
  27. Year.toint (&ok);
  28. if (ok = = false)
  29. {
  30. Txt_time->settext (tr ("ge Shi CuO wu"));
  31. return;
  32. }
  33. Month.toint (&ok);
  34. if (ok = = false)
  35. {
  36. Txt_time->settext (tr ("ge Shi CuO wu"));
  37. return;
  38. }
  39. Day.toint (&ok);
  40. if (ok = = false)
  41. {
  42. Txt_time->settext (tr ("ge Shi CuO wu"));
  43. return;
  44. }
  45. Hour.toint (&ok);
  46. if (ok = = false)
  47. {
  48. Txt_time->settext (tr ("ge Shi CuO wu"));
  49. return;
  50. }
  51. Min.toint (&ok);
  52. if (ok = = false)
  53. {
  54. Txt_time->settext (tr ("ge Shi CuO wu"));
  55. return;
  56. }
  57. str = "date-s" + month + "/" + Day + "/" + year;
  58. System (Str.tolatin1 (). data ());
  59. str = "date-s" + Hour + ":" + min + ":" + "00";
  60. System (Str.tolatin1 (). data ());
  61. //Force write to CMOS
  62. System ("clock-w");
  63. }
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.