当前位置: 代码迷 >> QT开发 >> qt 调用linux系统命令修改系统时间,该怎么处理
  详细解决方案

qt 调用linux系统命令修改系统时间,该怎么处理

热度:40   发布时间:2016-04-25 04:50:20.0
qt 调用linux系统命令修改系统时间

源码:
void MainWindow::pushButton_clicked()
{
  QString str1 = "date -s 04/30/2012";
  system(str1.toLatin1().data());

  QString str2 = "date -s 10:00:00";
  system(str2.toLatin1().data());
  //强制写入到CMOS
  system("clock -w");

}

运行结果:

date: 无法设置日期2012 04 30 ? 00:00:00 CST
: 不允许的操作
2012 04 29 10:00:00 CST
date: 无法设置日期: 不允许的操作
XView warning: Cannot load font '-b&h-lucida-medium-r-*-*-*-120-*-*-*-*-*-*' (Font package)
XView warning: Cannot load font '-b&h-lucida-medium-r-normal-sans-*-120-*-*-*-*-*-*' (Font package)
XView error: Cannot open connection to window server: :0.0 (Server package)


同步系统时钟与硬件时钟时间命令:

硬件时钟同步到系统时钟:hwclock --hctosys

如果:将system("clock -w"); 改为system("hwclock --hctosys");
则:
date: 无法设置日期2012 04 30 ? 00:00:00 CST
: 不允许的操作
2012 04 29 10:00:00 CST
date: 无法设置日期: 不允许的操作
Sorry, only the superuser can change the System Clock.


如果:将system("clock -w"); 改为system("hwclock -systohc");
则:
系统时钟同步到硬件时钟:hwclock -systohc
date: 无法设置日期: 不允许的操作
2012 04 30 ? 00:00:00 CST
2012 04 29 10:00:00 CST
date: 无法设置日期: 不允许的操作
hwclock:无效选项 -- y
hwclock - query and set the hardware clock (RTC)

Usage: hwclock [function] [options...]

Functions:
  -h | --help show this help
  -r | --show read hardware clock and print result
  --set set the rtc to the time given with --date
  -s | --hctosys set the system time from the hardware clock
  -w | --systohc set the hardware clock to the current system time
  --systz set the system time based on the current timezone
  --adjust adjust the rtc to account for systematic drift since
  the clock was last set or adjusted
  --getepoch print out the kernel's hardware clock epoch value
  --setepoch set the kernel's hardware clock epoch value to the 
  value given with --epoch
  --predict predict rtc reading at time given with --date
  -v | --version print out the version of hwclock to stdout

Options: 
  -u | --utc the hardware clock is kept in UTC
  --localtime the hardware clock is kept in local time
  -f | --rtc=path special /dev/... file to use instead of default
  --directisa access the ISA bus directly instead of /dev/rtc
  --badyear ignore rtc's year because the bios is broken
  --date specifies the time to which to set the hardware clock
  --epoch=year specifies the year which is the beginning of the 
  hardware clock's epoch value
  --noadjfile do not access /etc/adjtime. Requires the use of
  either --utc or --localtime
  --adjfile=path specifies the path to the adjust file (default is
  /etc/adjtime)
  --test do everything except actually updating the hardware
  clock or anything else
  -D | --debug debug mode
这是怎么回事呢?我QT 不是以root权限运行的
qt 怎样切换到root权限呢?

------解决方案--------------------
sudo ./yourapp

权限是对当前用户的限定,和程序本身无关。