Results 1 to 13 of 13

Thread: Using the Stdout reading in QProcess

  1. #1
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Using the Stdout reading in QProcess

    Hi guys
    Well I have been searching a still I can't solve this problem.
    I am trying to show the result of the pwd on a text edit.
    What I understood, was that I need to make a connection of slots, but when I run the program. It ends on the connect line. If someone could give me a hand?

    Qt Code:
    1. void MainWindow::on_pb_new_package_clicked()
    2. {
    3. QString install="pwd";
    4. process->execute(install);
    5. connect(process,SIGNAL(readReadyStandardOutput()),this, SLOT( _readstdoutput()));
    6. }
    7.  
    8. void MainWindow::_readstdoutput()
    9. {
    10. ui->textEdit->append(process->readAllStandardOutput());
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using the Stdout reading in QProcess

    you should do the connection before you call execute().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using the Stdout reading in QProcess

    Mmmmm
    If I understood you well.
    It should be like this?
    Qt Code:
    1. void MainWindow::on_pb_new_package_clicked()
    2. {
    3. QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
    4. connect(process,SIGNAL(readReadyStandardOutput()),this, SLOT( _readstdoutput()));
    5. process->execute(install);
    6. }
    7.  
    8. void MainWindow::_readstdoutput()
    9. {
    10. ui->textEdit->append(process->readAllStandardOutput());
    11. }
    To copy to clipboard, switch view to plain text mode 
    But I have my doubts. About this solution, the function _readstdoutput is going to append something that doesn't exist to the textedit?
    I tried it, and the program crashes when I make the conection

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using the Stdout reading in QProcess

    But I have my doubts. About this solution, the function _readstdoutput is going to append something that doesn't exist to the textedit?
    the connect() only connects the signal to the slot - the slot is not evoked at that stage.
    The slot will be called when the signals it is connected to is emitted, (in your case readReadyStandardOutput()) which only happens after you started your process, which is why before it didn't trigger, since you connected the slot after the signal has been emitted.

    I tried it, and the program crashes when I make the conection
    is 'process' initialized?
    Where/when do initialize it?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using the Stdout reading in QProcess

    Thanks about the slots. I have learned something new today.
    Yes process it is initialize. Here is the complete code.

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QProcess>
    6. #include <QMessageBox>
    7. #include <QtGui>
    8. #include <QFileDialog>
    9. #include <QFile>
    10. #include <QTreeWidget>
    11. #include <QDir>
    12. namespace Ui {
    13. class MainWindow;
    14. }
    15.  
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23. void _readstdoutput();
    24.  
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. QProcess *process;
    29.  
    30. private slots:
    31. void on_pb_new_package_clicked();
    32. void on_pushButton_clicked();
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::on_pb_new_package_clicked()
    17. {
    18. QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
    19. connect(process,SIGNAL(readReadyStandardOutput()),this, SLOT( _readstdoutput()));
    20. process->start(install);
    21. }
    22.  
    23. void MainWindow::_readstdoutput()
    24. {
    25. ui->textEdit->append(process->readAllStandardOutput());
    26. }
    To copy to clipboard, switch view to plain text mode 

    The program crashes when I make the connection. I suppose I am doing something wrong there.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using the Stdout reading in QProcess

    Yes process it is initialize.
    Where?
    I don't see where you initialized it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using the Stdout reading in QProcess

    Quote Originally Posted by high_flyer View Post
    Where?
    I don't see where you initialized it.
    On the .h

    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. QProcess *process;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using the Stdout reading in QProcess

    This is only the decleration of the pointer.
    You need somewhere in your code (constructor woudl be good):
    Qt Code:
    1. process = new QProcess();
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using the Stdout reading in QProcess

    Upps my mistake for not copying all the code

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::on_pushButton_clicked()
    17. {
    18. QStringList nombre_carpeta,archivos_carpetas;
    19. QString nombre,nombre_dir;
    20.  
    21. QFileDialog Dialog(this);
    22. Dialog.setFileMode(QFileDialog::Directory);
    23. Dialog.setViewMode(QFileDialog::Detail);
    24. if(Dialog.exec())
    25. {
    26. nombre_carpeta=Dialog.selectedFiles();
    27. nombre=nombre_carpeta.at(0);
    28. nombre_dir=nombre;
    29. nombre=nombre.section('/',-1);
    30. QString nombre_comprezo=nombre.section('-',0,0);
    31. QString nombre_version=nombre.section('-',1,1);
    32. nombre_comprezo=nombre_comprezo.append("_"+nombre_version);
    33. QString crear_tar="tar -czvf "+nombre.append(".tar.gz")+" "+nombre_carpeta.at(0);
    34. QString crear_origtar="tar -czvf "+nombre_comprezo.append(".tar.gz")+" "+nombre_carpeta.at(0);
    35. process=new QProcess(this); //-----> Here I did it
    36. process->start(crear_tar);
    37. process->execute(crear_origtar);
    38.  
    39. QDir carpeta_programa(nombre_dir);
    40. QList<QString>Informacion=carpeta_programa.entryList();
    41. QList<QTreeWidgetItem *> items;
    42. for(int i=0;i<carpeta_programa.count();i++)
    43. {
    44. //items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
    45.  
    46.  
    47. items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(Informacion)));
    48. Informacion.pop_front();
    49. }
    50. ui->treeWidget->insertTopLevelItems(0,items);
    51.  
    52. }
    53.  
    54.  
    55.  
    56. }
    57.  
    58. void MainWindow::on_pb_new_package_clicked()
    59. {
    60. QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
    61. connect(process,SIGNAL(),this, SLOT( _readstdoutput()));
    62. process->start(install);
    63. }
    64.  
    65. void MainWindow::_readstdoutput()
    66. {
    67. ui->textEdit->append(process->readAll());
    68. }
    To copy to clipboard, switch view to plain text mode 

    Also I think I am making bad use of the english language. When I mean it crashes, I refered that the program doesn't do what is supposed to do.

    This is the error it gives me

    Object::connect: No such signal QProcess::readReadyStandardOutput() in ../Using_QProcess/mainwindow.cpp:62
    Object::connect: (receiver name: 'MainWindow')

    That's way I was telling you that I think the problem was on the connect

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using the Stdout reading in QProcess

    you have a typo in your signal name is should be readyReadyStandardOutput() (ready not read)
    The warning tells you exactly what the problem is!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using the Stdout reading in QProcess

    The signal name is: readyReadStandardOutput (), not readReadyStandardOutput()

    LE: too late, high_flyer already answered while i was "formatting" my message

  12. #12
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using the Stdout reading in QProcess

    Problem solved!!!
    Thanks high_flyer
    PS. Google Traductor and my few english let me knows you were getting a little desesperate.
    But thanks

    It should be like this at the end

    Qt Code:
    1. void MainWindow::on_pb_new_package_clicked()
    2. {
    3. QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
    4. process->setProcessChannelMode(QProcess::MergedChannels);
    5. connect(process,SIGNAL(readyReadStandardOutput()),this, SLOT( _readstdoutput()));
    6. process->start(install);
    7. }
    8.  
    9. void MainWindow::_readstdoutput()
    10. {
    11. QByteArray ca=process->readAllStandardOutput();
    12. QString a(ca);
    13. ui->textEdit->setText(a);
    14. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jul 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using the Stdout reading in QProcess

    Thanks.
    It would be helpful for newbies like myself to have a complete and working designer project available, which I could then modify. I would be very grateful.
    Ekkehart

Similar Threads

  1. QProcess can read stderr but no stdout
    By mastupristi in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2010, 09:47
  2. Read most recent output from QProcess's stdout
    By Lawand in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2010, 22:29
  3. read stdout with QProcess under Windows
    By jlbrd in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2006, 17:29
  4. QProcess problem in accessing stdout
    By aruna in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 17:56
  5. Cannot get stdout from QProcess
    By johnny_sparx in forum Qt Programming
    Replies: 11
    Last Post: 3rd March 2006, 11:46

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.