0

Qt(C++)で本体の時刻を変更するというのを試しているのですが
以下のコードで試したところ、本体の時刻が変更されませんでした
Windowsで時刻の自動設定や、タイムゾーンの自動設定は外しています
Linuxでも同様のことをしたいのですが
本体の時刻の変更は何か別の関数を利用するのでしょうか

void MainWindow::on_pushButton_clicked()
{
    QDate *date = new QDate(2020,6,30); //日付設定
    QTime *tim = new QTime(19,10,50);   //時刻設定

    qDebug() << "日:" << date->toString();

    QDateTime *qd = new QDateTime(*date,*tim);  //日時を設定//★★★★日時を本体に設定する・・・はずだが反映されない
    qDebug() << "日時:" << qd->toString();
   
    QDateTimeEdit *dateTime = new QDateTimeEdit;
    dateTime->setDateTime(*qd);         
}
1
  • 本体とは OS (システム) のことを指していますか?また、Windows と Linux の両方で設定できるようにしたい、ということでしょうか?
    – cubick
    2020年6月29日 7:26

1 件の回答 1

2

これらの記事が該当するでしょう。つまり時刻を設定する権限が必要ということですね。


Windows:
How to change windows system time in Qt?
問:

I want to change my system time ,How can I change the Windows system time in Qt? I used this way,but failed!
システム時間を変更したいのですが、QtでWindowsシステム時間を変更するにはどうすればよいですか? 私はこの方法を使用しましたが、失敗しました!

#include <QApplication>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <QDateTime>
#include <QDebug>
using namespace std;
bool setDate(int,int,int);      
int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   MainWindow w;
   w.show();
   qDebug()<<QDateTime::currentDateTime()<<endl;    //before change time
   if(setDate(2015,1,1))                           //set time
   {
     qDebug()<<QDateTime::currentDateTime()<<endl;  //if succeed,output time
   }  
   return a.exec();
}
bool setDate(int year,int mon,int day)
{
   SYSTEMTIME st;
   GetSystemTime(&st);    // Win32 API get time
   st.wYear=year;         //set year
   st.wMonth=mon;         //set month
   st.wDay=day;           //set day

   return SetSystemTime(&st);    //Win32 API set time
}

答:

Changing the system time requires admin rights. That means you need to:
システム時刻を変更するには、管理者権限が必要です。 つまり、次のことを行う必要があります。

  • Add the requireAdministrator option to your manifest so that the program always has admin rights. That's a bad idea and you won't enjoy the UAC dialog every time you start.
    プログラムが常に管理者権限を持つように、マニフェストにrequireAdministratorオプションを追加します。 これは悪い考えであり、開始するたびにUACダイアログを楽しむことはできません。

  • Or, change the time by starting a separate process that runs as administrator. Another executable with the appropriate manifest, a process started with the runas shell verb, or one started with the COM elevation moniker.
    または、管理者として実行される別のプロセスを開始して時間を変更します。 適切なマニフェストを持つ別の実行可能ファイル、runasシェル動詞で開始されたプロセス、またはCOM昇格モニカで開始されたプロセス。

If this is gobbledygook to you, you need to read up on UAC. Start here:
これがごちゃごちゃしている場合は、UACを読む必要があります。 ここから始める:
https://msdn.microsoft.com/en-us/library/windows/desktop/dn742497(v=vs.85).aspx


Linux:
Set system clock with QT on linux
問:

How would we go about changing the system time on a linux system programatically using QT widget application ?
QTウィジェットアプリケーションを使用して、プログラムでLinuxシステムのシステム時間を変更するにはどうすればよいでしょうか。

答:

I found a simple solution. As my system is very minimalist i dont want to use things like dbus. As a root or sudoer this can be execute (fairly self explainatory )-
簡単な解決策を見つけました。 私のシステムはとてもミニマリストなので、dbusのようなものを使いたくありません。 ルートまたはsudoとしてこれを実行できます(かなり自明)-

QString string = dateTime.toString("\"yyyy-MM-dd hh:mm\"");
QString dateTimeString ("date -s ");
dateTimeString.append(string);
int systemDateTimeStatus= system(dateTimeString.toStdString().c_str());
if (systemDateTimeStatus == -1)
{
    qDebug() << "Failed to change date time";
}
int systemHwClockStatus = system("/sbin/hwclock -w");
if (systemHwClockStatus == -1 )
{
    qDebug() << "Failed to sync hardware clock";
}

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。