PDA

View Full Version : Using the Stdout reading in QProcess



DiegoTc
29th December 2010, 03:55
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?



void MainWindow::on_pb_new_package_clicked()
{
QString install="pwd";
process->execute(install);
connect(process,SIGNAL(readReadyStandardOutput()), this, SLOT( _readstdoutput()));
}

void MainWindow::_readstdoutput()
{
ui->textEdit->append(process->readAllStandardOutput());
}

high_flyer
29th December 2010, 09:03
you should do the connection before you call execute().

DiegoTc
29th December 2010, 13:50
Mmmmm
If I understood you well.
It should be like this?


void MainWindow::on_pb_new_package_clicked()
{
QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
connect(process,SIGNAL(readReadyStandardOutput()), this, SLOT( _readstdoutput()));
process->execute(install);
}

void MainWindow::_readstdoutput()
{
ui->textEdit->append(process->readAllStandardOutput());
}

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

high_flyer
29th December 2010, 14:05
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?

DiegoTc
29th December 2010, 14:18
Thanks about the slots. I have learned something new today.
Yes process it is initialize. Here is the complete code.



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>
#include <QMessageBox>
#include <QtGui>
#include <QFileDialog>
#include <QFile>
#include <QTreeWidget>
#include <QDir>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void _readstdoutput();


private:
Ui::MainWindow *ui;
QProcess *process;

private slots:
void on_pb_new_package_clicked();
void on_pushButton_clicked();
};

#endif // MAINWINDOW_H





#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pb_new_package_clicked()
{
QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
connect(process,SIGNAL(readReadyStandardOutput()), this, SLOT( _readstdoutput()));
process->start(install);
}

void MainWindow::_readstdoutput()
{
ui->textEdit->append(process->readAllStandardOutput());
}



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

high_flyer
29th December 2010, 14:20
Yes process it is initialize.
Where?
I don't see where you initialized it.

DiegoTc
29th December 2010, 14:27
Where?
I don't see where you initialized it.

On the .h


private:
Ui::MainWindow *ui;
QProcess *process;

high_flyer
29th December 2010, 14:34
This is only the decleration of the pointer.
You need somewhere in your code (constructor woudl be good):


process = new QProcess();

DiegoTc
29th December 2010, 14:47
Upps my mistake for not copying all the code



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
QStringList nombre_carpeta,archivos_carpetas;
QString nombre,nombre_dir;

QFileDialog Dialog(this);
Dialog.setFileMode(QFileDialog::Directory);
Dialog.setViewMode(QFileDialog::Detail);
if(Dialog.exec())
{
nombre_carpeta=Dialog.selectedFiles();
nombre=nombre_carpeta.at(0);
nombre_dir=nombre;
nombre=nombre.section('/',-1);
QString nombre_comprezo=nombre.section('-',0,0);
QString nombre_version=nombre.section('-',1,1);
nombre_comprezo=nombre_comprezo.append("_"+nombre_version);
QString crear_tar="tar -czvf "+nombre.append(".tar.gz")+" "+nombre_carpeta.at(0);
QString crear_origtar="tar -czvf "+nombre_comprezo.append(".tar.gz")+" "+nombre_carpeta.at(0);
process=new QProcess(this); //-----> Here I did it
process->start(crear_tar);
process->execute(crear_origtar);

QDir carpeta_programa(nombre_dir);
QList<QString>Informacion=carpeta_programa.entryList();
QList<QTreeWidgetItem *> items;
for(int i=0;i<carpeta_programa.count();i++)
{
//items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));


items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(Informacion)));
Informacion.pop_front();
}
ui->treeWidget->insertTopLevelItems(0,items);

}



}

void MainWindow::on_pb_new_package_clicked()
{
QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
connect(process,SIGNAL(),this, SLOT( _readstdoutput()));
process->start(install);
}

void MainWindow::_readstdoutput()
{
ui->textEdit->append(process->readAll());
}



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

high_flyer
29th December 2010, 14:55
you have a typo in your signal name is should be readyReadyStandardOutput() (ready not read)
The warning tells you exactly what the problem is!

Zlatomir
29th December 2010, 15:02
The signal name is: readyReadStandardOutput (), not readReadyStandardOutput()

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

DiegoTc
29th December 2010, 15:07
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



void MainWindow::on_pb_new_package_clicked()
{
QString install="pwd";//"gksudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder";
process->setProcessChannelMode(QProcess::MergedChannels);
connect(process,SIGNAL(readyReadStandardOutput()), this, SLOT( _readstdoutput()));
process->start(install);
}

void MainWindow::_readstdoutput()
{
QByteArray ca=process->readAllStandardOutput();
QString a(ca);
ui->textEdit->setText(a);
}

ekkehart
10th July 2011, 07:36
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