Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QByteArray to QString.
QtWS25 Last Chance

[Solved]QByteArray to QString.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 45.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bmps
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      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.

      http://anavi.org/

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bmps
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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).

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tyagiait
              wrote on last edited by
              #6

              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......

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tyagiait
                  wrote on last edited by
                  #8

                  thank u......

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved