Results 1 to 2 of 2

Thread: Qprocess

  1. #1
    Join Date
    Aug 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qprocess

    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
    Last edited by ricky; 2nd August 2009 at 21:34.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qprocess

    You are redefining proc:

    Qt Code:
    1. void Dialog::connetti()
    2. {
    3. QProcess *proc = new QProcess(this);
    4. ...
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void Dialog::connetti()
    2. {
    3. proc = new QProcess(this);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  2. QProcess exitStatus()
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:51
  3. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  4. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  5. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30

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.