[Solved]QByteArray to QString.



  • I have a Widget project with a Push Button and a Text Edit wherein 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 this 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); //to convert byte array to string

    QPushButton *KernelName= new QPushButton();
    ui->textEdit->setText(command);
    @
    Let me know where am going wrong as when i run it the output is not seen in the textEdit.

    [edit: please add @ code tags, Eddy]



  • To constructs a string initialized with a byte array content just use:
    @QString::QString ( const QByteArray & ba ) @
    For more info please "click here":http://doc.qt.nokia.com/latest/qstring.html#QString-9

    Please make sure that your issue is not related to something else in your source code.



  • The problem is not with the byte array or the QString, but with your process. "QProcess::execute() ":http://doc.qt.nokia.com/4.7/qprocess.html#execute-2 is a static method. It does not need an object to be called, and - more important for your case - it does not hand back the standard output and and standard error are redirected to the respective channels of your program. You cannot catch the result.

    A solution to your problem would be this snippet:

    @
    QProcess proc;
    proc.start("uname -s"); // start returns immediatly
    proc.waitForFinished(-1); // wait until the process has finished
    QByteArray result = proc.readAllStandardOutput();
    QString command(result); //to convert byte array to string
    @



  • Thanks Mr.Volker that worked. So silly of me.



  • No problem. It's easy to miss and the the code completion might be another source of problem in that case :-)

    Glad to know it works now. If you want, mark the topic as solved by prepending [Solved] to the title (just hit the edit link right to the very first post of this thread).



  • Hello,

    My problem question is same but i want to show the output in continous manner.
    Like if i m installing a software then first line of process shown in terminal to be displayed in textbox and then the second one while the process still running.

    how i m gona achieve this...
    help me out in this.....
    Thanks......


  • Lifetime Qt Champion

    Hi and welcome to DevNet,

    Please, start a new thread describing your problem rather than posting in an old solved one (You can mention this thread in your new post though)



  • thank u......


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.