簡體   English   中英

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

[英]QString& QString::operator=(const QByteArray&)' is private

我正在嘗試從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 

只需執行以下操作:

QByteArray name = process.readAllStandardOutput();
QString username = QString::fromRawData(name.data(), name.size()); 
QProcess process;
process.start(QString::fromStdString("whoami"));
process.waitForFinished(-1); // this could be omitted
QTextStream txtStream(&process);
QString username = txtStream.readLine();

注意默認情況下, QTextStream使用默認的區域設置字符串編碼。 您可以使用QTextStream::setCodec更改字符串編碼(UTF-8,Windows-1250,UCS或任何您需要的,系統區域設置中的默認編解碼器通常是最佳選擇)。 它還允許您以流方式處理數據,這總是很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM