PDA

View Full Version : Qprocess



ricky
2nd August 2009, 21:10
Hi everybody.
It's mine first thread and i'm not English spooken for those sorry if i write wrong.
I'm try to make a Qprocess on mine app, the app have 3 Files, main.cpp, mainwindow.cpp and mainwindow.h
On main.cpp the normal sequence
:
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog dialog;
return dialog.exec();
}

on Mainwindow.h the definition of widget :

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

#include <QDialog>

class QAction;
class QDialogButtonBox;
class QGroupBox;
class QPushButton;
class QTextEdit;
class QProcess;

class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog();
public slots:
void readFromStdout();
void scrollToTop();
void connetti();
void sconnetti();
void sincronizza();
void backup();
private:
void createMenu();
void createHorizontalGroupBox();
void createGridGroupBox();
void createFormGroupBox();
QGroupBox *horizontalGroupBox;
QTextEdit *bigEditor;
QPushButton *buttons[4];
QDialogButtonBox *buttonBox;
QProcess *proc;
QString output , argument , program ;
};

And last the mainwindow.cpp with definition of member and slot :

#include "mainwindow.h"
#include <QtGui>
Dialog :: Dialog()
{
createHorizontalGroupBox();
bigEditor = new QTextEdit;
bigEditor->setPlainText(tr("This widget takes up all the remaining space "
"in the top-level layout."));
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(bigEditor);
mainLayout->addWidget(horizontalGroupBox);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("Basic Layouts"));
connect(buttons[0], SIGNAL(clicked()), this, SLOT(connetti()));
connect(buttons[1], SIGNAL(clicked()), this, SLOT(sconnetti()));
connect(buttons[2], SIGNAL(clicked()), this, SLOT(sincronizza()));
connect(buttons[3], SIGNAL(clicked()), this, SLOT(backup()));
}
void Dialog::createHorizontalGroupBox()
{
horizontalGroupBox = new QGroupBox(tr("HTC Touch Pro"));
QHBoxLayout *layout = new QHBoxLayout;
buttons[0] = new QPushButton(tr("Connetti"));
buttons[1] = new QPushButton(tr("Sconnetti"));
buttons[2] = new QPushButton(tr("Sincronizza"));
buttons[3] = new QPushButton(tr("Back Up"));
for (int i = 0; i < 4; ++i)
{
layout->addWidget(buttons[i]);
}
horizontalGroupBox->setLayout(layout);
}
void Dialog::readFromStdout()
{
bigEditor->append( "Adesso sono qua evviva evviva" );
output += proc->readAll() + "\n";
bigEditor->append( output );
}
void Dialog::scrollToTop()
{
bigEditor->append( "NNNNAHA!" );
}
void Dialog::connetti()
{
QProcess *proc = new QProcess(this);

QString program = "ls";
QStringList arguments;
arguments << "-la" << "" ;
proc->setProcessChannelMode(QProcess::SeparateChannels);
proc->setReadChannel(QProcess::StandardOutput);
output = "";
proc->start(program, arguments);
connect( proc, SIGNAL(readyReadStandardOutput ()),this, SLOT(readFromStdout()) );
connect( proc, SIGNAL(finished(int)),this, SLOT(scrollToTop()) );
bigEditor->append( "CONNESSO" );
}
void Dialog::sconnetti()
{
bigEditor->append( "SCONNESSO" );
}
void Dialog::sincronizza()
{
bigEditor->append( "SINCRONIZZATO" );
}
void Dialog::backup()
{
bigEditor->append( "BACKUP" );
}

Ok, and now the problem. When i Click on first button 'Connetti' i don't see the result of command. Where i make the error, and which is the error ?
Tks for now and for your kind attention.
Ricky

victor.fernandez
3rd August 2009, 09:32
You are redefining proc:


void Dialog::connetti()
{
QProcess *proc = new QProcess(this);
...

Just remove QProcess * to use the member variable instead of creating a local variable that overrides the member variable:


void Dialog::connetti()
{
proc = new QProcess(this);
...

In order to avoid confusions, I recommend you to use the prefix "m_" for member variable names. That way you always know whether it's a member variable or a local variable.