在Qt中使用windows进程时的问题

Issue when using a windows process in Qt

本文关键字:进程 问题 windows Qt      更新时间:2023-10-16

我试图在Qt Creator中做一个程序,在那里我想更新一个。hex文件到arduino。我已经在c#中做了这个程序,但是在QT中调用avrdude时我遇到了一些问题。

我的第一次尝试:

process = new QProcess(this);
process->start("avrdude -Cavrdude.conf.txt -v -v -v -v -patmega328p -arduino -PCOM7 -b115200 -D -Uflash:w:Blink.cpp.hex:i", 0);
process->waitForFinished(-1);
QByteArray stdout = process.readAllStandardOutput();
QByteArray stderr = process->readAllStandardError();

第二次尝试:

QString exePath = "avrdude";
QString arguments = "-Cavrdude.conf.txt -v -v -v -v -patmega328p -carduino -PCOM7 -b115200 -D -Uflash:w:Blink.cpp.hex:i";
#ifdef Q_OS_WIN32
ShellExecuteW(NULL, NULL, (LPCWSTR)exePath.toStdWString().data(), (LPCWSTR)arguments.toStdWString().data(), NULL, SW_HIDE);
#endif

我想知道是否有人知道为什么我的代码什么都不做,或者我可以使用的一些解决方案。

avrdude是否在路径中?如果不是,您可以在执行进程之前调用setWorkingDirectory,或者将其添加到路径中。为了检查它,打开命令行,cd进入Qt应用程序的二进制文件夹,并输入命令:

avrdude -Cavrdude.conf.txt -v -v -v -v -patmega328p -arduino -PCOM7 -b115200 -D -Uflash:w:Blink.cpp.hex:i", 0

它运行了吗?如果不是,你得到的误差是多少?

您也可以使用error函数来获取最后一个错误

不妨试试这个:

QProcess::execute( /*program*/, /*arguments*/ );