我試圖從QProcess
讀取標準輸出作爲QString
,其中傳遞的參數是一個linux命令。 linux命令爲我提供了linux用戶名。當我將參數傳遞給QProcess
時,我期望輸出成爲我的linux用戶名。在這方面,我要讀的標準輸出和得到的結果作爲QString
,但我得到的錯誤:QString&QString :: operator =(const QByteArray&)'是私有的
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
這很奇怪,[this reference](http://doc.qt.io/qt-5/qstring.html)表示['operator =(const QByteArray&)'](http://doc.qt .io/qt-5/qstring.html#operator-eq-5)功能是公共的。你正在做一些你現在做的事嗎?你能否請創建一個[最小,***完整***和可驗證示例](http://stackoverflow.com/help/mcve)並向我們展示? –
此外,您向我們顯示的錯誤實際上並不符合您向我們顯示的代碼。對於'QString username = name',它應該是'QString'的'public' [* constructor *](http://doc.qt.io/qt-5/qstring.html#QString-8)應該使用的,而不是賦值操作符。再次,請向我們展示[MCVE](https://stackoverflow.com/help/mcve)以及實際錯誤(完整,完整且無需修改)。 –
username是一個在.h文件中聲明爲靜態QString用戶名的類變量。在上面的代碼中: - 使用了username = name。因此調用賦值運算符。 –