[Qt-interest] qprocess related with qprogressbar

Sean Harmer sean.harmer at maps-technology.com
Fri Jun 26 00:29:01 CEST 2009


Hi,

mierdatutis mi wrote:
> Many thanks to all.
>
> I will try to capture the stdout of the shell script and parse it in 
> order to update the progress bar.
>
> I see that for I capture the stdout I have to use the signal 
> "readyReadStandardOutput"
>
>
> I declare a Qprocess in the funciton MainWindow but when I try to 
> compile it gives me an error when I do:
>
> proc2->start("/home/david/seguridad/verdadero/pruebon.sh", 
> QStringList() << strs );
>
>
> in other function that it's a slot of a ListWidget Signal. The 
> compiler says me:
>
> src/mawindindowimpl.cpp:87:error: 'proc2' not declared in this scope
>
>
> I must declare : QProcess *proc2 = new QProcess(); in all functions???
>
> Many thanks and sorry for my english!
>
No, we are no venturing into basic c++ land and not related directly to 
Qt. You need to declare your QProcess object in your class declaration. 
I would then instantiate it in the constructor of your class and then 
use it in a member function of that class. For e.g.

In your .h file

class QProcess;

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
    MainWindow( QWidget* parent = 0 );
    ~MainWindow();

public slots:
    void doSomething();

protected slots:
    void processStarted();
    void updateProgress();

private:
    QProcess* m_proc;
};

Then in your .cpp file

MainWindow::MainWindow( QWidget* parent )
: QMainWindow( parent ),
  m_proc( 0 )
{
    m_proc = new QProcess( this );
    connect( m_proc, SIGNAL( started() ), SLOT( processStarted() ) );
    connect( m_proc, SIGNAL( readyReadStandardOut() ), SLOT( 
updateProgress() ) );
}

MainWindow::~MainWindow
{
    // No need to delete m_proc as Qt object model does this for us
}

void MainWindow::doSomething()
{
    // set up arguments
    ...
    m_proc->start("/home/david/seguridad/verdadero/pruebon.sh", 
QStringList() << strs );
}

void MainWindow::processStarted()
{
    // When this gets called the process has started
    qDebug() << "Process started successfully";
}

void MainWindow::updateStatus()
{
    // Get output from process
    QByteArray output = m_proc->readAllStandardOut();

    // Parse it as needed
    int percent;
    ...

    // Update progress bar
    m_progress->setValue( percent );
}

That should give you an idea of what to do.

HTH,

Sean



More information about the Qt-interest-old mailing list