Maybe there are extra line breaks or Spaces to get the result, so this won't work. Thanks so much for answering!!
0 Answer
Maybe there are extra line breaks or Spaces to get the result, so this won't work. Thanks so much for answering!!
solved, thank you very much!
Part of this answer references CHATGPT, GPT_Pro better solution
How do you compare the results of running Linux commands? To compare, we first need to get the results of executing a Linux command, then store those results in a variable, and finally use an if statement to compare the values of the two variables.
Example:
QProcess process;
process.start("cat /sys/class/gpio/value");
process.waitForFinished();
QString result = process.readAllStandardOutput();
if (result == "1") {
// do something
} else {
// do something else
}
In the code above, we first define a process object using the QProcess class, then use the start() function to execute Linux commands, and use the waitForFinished() function to ensure that the program continues. Finally, the readAllStandardOutput() function is used to read the standard output of the Linux command and store the value in the result variable. Finally, we use an if statement to compare whether the value of the result variable is 1. If it is, the corresponding code is executed, otherwise the other code is executed.
the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > when comparing, need to pay attention to the consistency of the data type. In this example, result is of type QString, and 1 is of type integer.
You can convert result to an integer number type and then compare. For example:
QProcess process;
process.start("cat /sys/class/gpio/value");
process.waitForFinished();
QString result = process.readAllStandardOutput().trimmed(); // 去掉两端的空白字符
int resultInt = result.toInt(); // 将result转换成整型数字
if (resultInt == 1) {
// do something
}
Note that when converting result to an integer number, you need to trim the whitespace characters at both ends using the trimmed() function first, since there may be additional characters such as line breaks or Spaces when executing Linux commands.
这家伙很懒,什么都没留下...