Results 1 to 7 of 7

Thread: QByteArray to Qstring

  1. #1
    Join Date
    Nov 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QByteArray to Qstring

    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.
    Qt Code:
    1. QByteArray result = proc.readAllStandardOutput();
    2. QString command(result);
    3. result=command.toAscii();
    4. QPushButton *KernelName= new QPushButton();
    5. ui->textEdit->setText(command);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 16th November 2011 at 13:55. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QByteArray to Qstring

    skip the line
    Qt Code:
    1. result=command.toAscii();
    To copy to clipboard, switch view to plain text mode 
    and it should work.

  3. #3
    Join Date
    Nov 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QByteArray to Qstring

    It still shows the output only in the Application Output when i run and click on the PushButton. Where am i goin wrong???

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QByteArray to Qstring

    Is result correct? What do you see if you put
    Qt Code:
    1. qWarning() << result;
    To copy to clipboard, switch view to plain text mode 
    afterwards?

    EDIT:

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

  5. #5
    Join Date
    Nov 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QByteArray to Qstring

    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.
    Qt Code:
    1. QProcess proc;
    2. proc.execute("uname -s");
    3. QByteArray result = proc.readAllStandardOutput();
    4. QString command(result);
    5.  
    6. QPushButton *KernelName= new QPushButton();
    7. ui->textEdit->setText(command);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 16th November 2011 at 18:56. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QByteArray to Qstring

    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!
    Qt Code:
    1. QProcess proc;
    2. proc.start("uname", QStringList() << "-s");
    3. proc.waitForFinished();
    4. QString command(proc.readAllStandardOutput());
    5. command = command.trimmed();
    6. qWarning() << command;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Nov 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QByteArray to Qstring

    Thanks a lot Sir.That worked. Am loving QT. Works Great.

Similar Threads

  1. Problem with QString and QByteArray
    By ts66 in forum Newbie
    Replies: 2
    Last Post: 10th May 2011, 21:30
  2. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  3. QByteArray to QString
    By hvitual in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2009, 10:43
  4. QByteArray to QString
    By babu198649 in forum Newbie
    Replies: 7
    Last Post: 6th December 2007, 13:08
  5. How to get QByteArray from QString in Qt3
    By joseph in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2007, 09:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.