PDA

View Full Version : QByteArray to Qstring



bmps
16th November 2011, 13:54
Have a QProcess which gets a command output from my Ubuntu system. I have a pushbutton which executes this QProcess and does the work perfectly and i see the output in Application Window.. But i have a Text Edit in front of this Pushbutton where i need the output of Qprocess to be pasted. But this is not happening. I am doing a ReadallStandard Output which converts to QByteArray form. I guess this is the problem as its not being converted to string format.How do i get the QbyteArray to String format???
Here is the code i am using.Let me know what is wrong.

QByteArray result = proc.readAllStandardOutput();
QString command(result);
result=command.toAscii();
QPushButton *KernelName= new QPushButton();
ui->textEdit->setText(command);
}

Lykurg
16th November 2011, 13:56
skip the line
result=command.toAscii(); and it should work.

bmps
16th November 2011, 14:14
It still shows the output only in the Application Output when i run and click on the PushButton. Where am i goin wrong???

Lykurg
16th November 2011, 15:38
Is result correct? What do you see if you put
qWarning() << result; afterwards?

EDIT:

Do you call that snippet after the finished() signal was emitted?

bmps
16th November 2011, 18:11
This is what i am trying to do.
I have a Widget project with a Push Button and a Text Edit wherin when the button is clicked the Qprocess executes the command and gives the output.
This is the code i have written. I can see the QProcess executing the command and fetching the details in the Application Output Window. I want to fetch the Output as a String and see that output in the Text Edit.

QProcess proc;
proc.execute("uname -s");
QByteArray result = proc.readAllStandardOutput();
QString command(result);

QPushButton *KernelName= new QPushButton();
ui->textEdit->setText(command);

Lykurg
16th November 2011, 18:55
Well, you have to use start(), not execute(). Then either use the signal or wait till execution is finished, but that will block your ui. But since uname is fast, that shouldn't be a problem. Also please use the [code]-tags!

QProcess proc;
proc.start("uname", QStringList() << "-s");
proc.waitForFinished();
QString command(proc.readAllStandardOutput());
command = command.trimmed();
qWarning() << command;

bmps
16th November 2011, 20:02
Thanks a lot Sir.That worked. Am loving QT. Works Great.