[Qt-interest] qprocess related with qprogressbar
mierdatutis mi
mmm286 at gmail.com
Fri Jun 26 07:45:08 CEST 2009
Many thanks Sean!!
I try with this code but the compiler gives me an error:
"Program received signal SIGSERV. Segmentation fault"
#include <QMainWindow>
#include "ui_mainwindow.h"
//
class MainWindowImpl : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
private slots:
void cambio(QListWidgetItem*);
void salida();
public slots:
void doSomething();
private:
QProcess *proc2;
};
#include "mainwindowimpl.h"
#include "mainwindowimpl.h"
#include <QProcess>
#include <QtGui>
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QTextEdit>
#include <QListWidgetItem>
#include <QTextBrowser>
#include <QFileInfoList>
#include <iostream>
#include <QThread>
#include <QString>
#include <stdio.h>
#include <QProgressDialog>
#include <QFileDialog>
#include <QImage>
#include <QByteArray>
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);
QDir dir;
//showFullScreen();
QProcess *proc2 = new QProcess();
QStringList strs;
QString directorio = "/home/david/COMPLETADAS/PELIS";
QDir listado(directorio);
listado.setFilter(QDir::Dirs | QDir::Hidden | QDir::NoSymLinks);
listado.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = listado.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
lista->insertItem(i,fileInfo.fileName());
}
connect(proc2, SIGNAL(readyReadStandardOutput()),this, SLOT(salida()));
connect(lista, SIGNAL( itemClicked(QListWidgetItem*) ), this,
SLOT(cambio(QListWidgetItem*) ) );
}
void MainWindowImpl::cambio(QListWidgetItem * item )
{
doSomething();
}
void MainWindowImpl::doSomething()
{
QString str=lista->currentItem()->text();
qDebug() << lista->currentItem()->text();
proc2->start("/home/david/seguridad/verdadero/sino.sh",QStringList() << str
);
}
void MainWindowImpl::salida()
{
//QProcess *proc2 = new QProcess();
QStringList strs;
QByteArray result=proc2->readAllStandardOutput();
qDebug() << proc2->readAllStandardOutput();
QStringList lines = QString(result).split("\n");
foreach (QString line, lines) {
texto->append(line);
}
}
Could you help me please? I search in google but I don't find nothing :-(
Many thanks and sorry for my english!!
2009/6/26 Sean Harmer <sean.harmer at maps-technology.com>
> 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
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090626/4da7e86c/attachment.html
More information about the Qt-interest-old
mailing list