use qprocess so text is printed live

This commit is contained in:
Jason Wen
2024-05-31 23:58:23 -04:00
parent 96506a9097
commit 7703ea8c51
+23 -13
View File
@@ -5,6 +5,7 @@
#include <QVBoxLayout>
#include <QWidget>
#include <QtConcurrent>
#include <QProcess>
#include <cstdio>
#include <sstream>
@@ -55,7 +56,26 @@ int main(int argc, char *argv[]) {
update_btn->setText(QObject::tr("Update"));
#ifdef __aarch64__
btn->setText(QObject::tr("Reboot"));
QFutureWatcher<void> watcher;
QProcess *command = new QProcess();
QObject::connect(command, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus){
btn->setEnabled(true);
update_btn->setEnabled(true);
update_btn->setText("Ready to Reboot");
command->deleteLater(); // Delete QProcess after it finishes
});
QObject::connect(command, &QProcess::readyReadStandardOutput, [=]() {
QByteArray result = command->readAllStandardOutput();
label->setText(label->text() + "\n" + result); // appends the text to the QLabel
scroll->update();
});
QObject::connect(command, &QProcess::readyReadStandardError, [=]() {
QByteArray result = command->readAllStandardError();
label->setText(label->text() + "\n" + result); // appends the text to the QLabel
scroll->update();
});
QObject::connect(btn, &QPushButton::clicked, [=]() {
Hardware::reboot();
});
@@ -78,18 +98,8 @@ int main(int argc, char *argv[]) {
}
const std::string cmd = to_home_dir + "; " + remote_cmd + "; " + fetch_remote + "; " + reset_branch;
QFuture<void> future = QtConcurrent::run([=]() {
std::string output = executeCommand(cmd.c_str());
LOGW("CHECK OUTPUT PLS\n%s", output.c_str());
QMetaObject::invokeMethod(label, "setText", Qt::QueuedConnection,
Q_ARG(QString, QString::fromStdString(output)));
});
QObject::connect(&watcher, &QFutureWatcher<void>::finished, [=]() {
btn->setEnabled(true);
update_btn->setEnabled(true);
update_btn->setText("Ready to Reboot");
});
watcher.setFuture(future);
QString c = QString::fromStdString(cmd);
command->start(c);
});
#else
update_btn->setEnabled(false);