我有以下代码:
QProcess* proc = new QProcess();
proc->start("cmd.exe");
proc->write("ssh\n");
proc->closeWriteChannel();
proc->waitForFinished();
QTextCodec *codec = QTextCodec::codecForName("IBM 866");
ui->textEditNormal->setText(codec->toUnicode(proc->readAllStandardOutput()));
ui->textEditError->setText(codec->toUnicode(proc->readAllStandardError()));
用
StandartError
编写:'ssh' is not recognized as an internal or external command
如果我用
cmd.exe
重复所有内容,那么一切都很好如何解决?
最佳答案
也许您应该等待进程开始后再进行写入。下面的示例执行类似的操作。我没有安装ssh,所以我在Windows计算机上发出了dir
。就试一试吧。
#include <QApplication>
#include <QDebug>
#include <QTextBrowser>
#include <QProcess>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto process = new QProcess;
auto view = new QTextBrowser;
process->setWorkingDirectory("C:/Temp");
QObject::connect(process, &QProcess::readyReadStandardOutput, [process,view]() {
auto output=process->readAllStandardOutput();
view->append(output);
});
QObject::connect(process, &QProcess::readyReadStandardError, [process,view]() {
auto output=process->readAllStandardError();
view->append(output);
});
process->start("cmd.exe");
process->setProcessChannelMode(QProcess::MergedChannels);
process->waitForStarted();
process->write("dir\n");
qDebug() << process->error();
view->show();
return a.exec();
}
关于c++ - SSH在QProcess中不起作用,但在控制台中起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57505612/