QString的&安培; QString :: operator =(const QByteArray&)'是私有的

时间:2017-08-04 11:29:06

标签: c++ qt

我正在尝试将QProcess的标准输出读作QString,其中传递的参数是linux命令。 linux命令给我linux用户名。当我将参数传递给QProcess时,我希望输出为我的linux用户名。这样做我必须读取标准输出并将结果作为QString但我收到错误:

QString& QString::operator=(const QByteArray&)' is private.

我的代码:

QProcess process;
process.start(QString::fromStdString("whoami"));
process.waitForFinished(-1); // will wait forever until finished
QByteArray name = process.readAllStandardOutput();
QString username = name;  //Error here saying 

2 个答案:

答案 0 :(得分:0)

只需这样做:

QByteArray name = process.readAllStandardOutput();
QString username = QString::fromRawData(name.data(), name.size()); 

答案 1 :(得分:0)

QProcess process;
process.start(QString::fromStdString("whoami"));
process.waitForFinished(-1); // this could be omitted
QTextStream txtStream(&process);
QString username = txtStream.readLine();

Note QTextStream by default is using default locale string encoding what is preferred. You can use QTextStream::setCodec to change string encoding (UTF-8, Windows-1250, UCS or whatever you need, default codec from system locale is usually best choice). It also allows you to process data in streamed manner and this is always good.