「Qtの基礎 - シャットダウン」の版間の差分

ナビゲーションに移動 検索に移動
223行目: 223行目:
  void Converter::suspend()
  void Converter::suspend()
  {
  {
     auto trySuspendCommand = [](const QString &command, const QStringList &arguments) {
     auto trySuspendCommand = [this](const QString &command, const QStringList &arguments, bool useAuth = false) {
       QProcess process;
       QProcess process;
       process.start(command, arguments);
       if (useAuth) {
       process.waitForFinished(5000);  // 5秒待機
          bool ok;
       return process.exitCode() == 0;
          QString password = QInputDialog::getText(this, "rootパスワード",
                                                  "サスペンドを実行するためのsudoパスワードを入力:",
                                                  QLineEdit::Password, QString(), &ok);
          if (!ok || password.isEmpty()) {
            qWarning() << "サスペンドがキャンセルされました: パスワードが提供されていません";
            return false;
          }
          process.start("sudo", QStringList() << "-S" << command << arguments);
          if (!process.waitForStarted()) {
            qCritical() << "sudoプロセスの開始に失敗: " << process.errorString();
            return false;
          }
          process.write(password.toUtf8() + "\n");
       }
      else {
          process.start(command, arguments);
      }
   
      // 5秒待機
       if (!process.waitForFinished(5000)) {
          qWarning() << "サスペンドプロセスが完了しませんでした: " << process.errorString();
          return false;
      }
      if (process.exitCode() != 0) {
          qWarning() << "サスペンドに失敗しました。終了コード: " << process.exitCode();
          qWarning() << "エラー出力: " << process.readAllStandardError();
          return false;
      }
      return true;
     };
     };
   
   

案内メニュー