PDA

View Full Version : Calling external programs?



Hossie
17th May 2008, 15:32
Hi,

I'm new to QT and I can't get calling external programs to work. I have this:


class mainwindow : public QMainWindow, public Ui::MainWindow{
Q_OBJECT

private:
[...]
QProcess* proc;
[...]



void mainwindow::doit(){
QString program="/usr/bin/echo";
QStringList arguments;
arguments << "test";
proc=new QProcess(parent);

connect(proc, SIGNAL(readyReadStdout()), this, SLOT(readFromStdout()));
if(!proc->start(program, arguments)){
QMessageBox::critical(0,"fatal error", "could not start", "quit");
exit(-1);
}
}

void mainwindow::readFromStdout(){
textEdit->append(???);
}

First, the "start" does not work:


mainwindow.cpp:29: error: no matching function for call to ‘QProcess::QProcess(<unresolved overloaded function type>)’

Second, what to append to textEdit?

Can someone help?

jpn
17th May 2008, 16:04
mainwindow.cpp:29: error: no matching function for call to ‘QProcess::QProcess(<unresolved overloaded function type>)’

Did you #include <QProcess>?



Second, what to append to textEdit?

QProcess::readAllStandardOutput()

Hossie
17th May 2008, 16:16
mainwindow.cpp:


#include <QtGui>
#include <QProcess>
#include "mainwindow.h"

jpn
17th May 2008, 16:20
Where is variable named "parent" declared? Did you mean "this" or "parent()"?

Hossie
17th May 2008, 16:23
That part was just a paste. I looked at the constructor again, and it seems all arguments are optional. If I change it to


proc=new QProcess();

At least that error disappeared. Now I got an even worse one. :(


mainwindow.cpp: In member function ‘void mainwindow::doit()’:
mainwindow.cpp:33: error: could not convert ‘((mainwindow*)this)->mainwindow::proc-> QProcess::start(((const QString&)((const QString*)(& program))), ((const QStringList&)((const QStringList*)(& arguments))), QFlags<QIODevice::OpenModeFlag>(ReadWrite))’ to ‘bool’

:eek::eek::eek:

€: Without the if part, it compiles. But it does not do what expected:


Object::connect: No such signal QProcess::readyReadStdout()
Object::connect: (receiver name: 'MainWindow')

jpn
17th May 2008, 16:27
That part was just a paste. I looked at the constructor again, and it seems all arguments are optional. If I change it to


proc=new QProcess();

At least that error disappeared.
Every QObject (QProcess is a QObject) takes a parent parameter for a reason (http://doc.trolltech.com/4.4/objecttrees.html). Make sure to pass a proper parent or delete the object yourself. Otherwise you'll get a memory leak.



Now I got an even worse one. :(


mainwindow.cpp: In member function ‘void mainwindow::doit()’:
mainwindow.cpp:33: error: could not convert ‘((mainwindow*)this)->mainwindow::proc-> QProcess::start(((const QString&)((const QString*)(& program))), ((const QStringList&)((const QStringList*)(& arguments))), QFlags<QIODevice::OpenModeFlag>(ReadWrite))’ to ‘bool’
Take a closer look at QProcess::start() docs. It's a void function. No bool is returned.

jpn
17th May 2008, 16:29
€: Without the if part, it compiles. But it does not do what expected:


Object::connect: No such signal QProcess::readyReadStdout()
Object::connect: (receiver name: 'MainWindow')

Again, see QProcess docs and see what signals it offers. Don't guess, read the great docs! :)

PS. And make sure you read correct docs. Qt 3 and Qt 4 are different.

Hossie
17th May 2008, 16:35
Ok, thanks, it works now. I still dont quite understand where to get "parent" from. Should I save the parent argument from the constructor and take that for callin QProcess?

jpn
17th May 2008, 16:51
I still dont quite understand where to get "parent" from. Should I save the parent argument from the constructor and take that for callin QProcess?
QObjects organize themselves in object trees (http://doc.trolltech.com/4.4/objecttrees.html). Every QObject can access its parent any time via QObject::parent(). Do you want the process object to be deleted when the parent of the window is deleted? Most likely not because top level windows usually don't have any parent. Do you want the process object to be deleted when the window is deleted? If yes, pass the window (ie. "this") as a parent.

Hossie
17th May 2008, 16:58
Ok, now I think everything works. Thank you!! :)

Hossie
17th May 2008, 17:15
€: Nevermind. It's working. :)

jpn
17th May 2008, 17:18
What does QProcess::waitForFinished() return? If it returns false, what does QProcess::error() return?

Hossie
17th May 2008, 17:19
Ok you answered before I edited. You are too fast. :o

I forgot the proc->start() :o:o:o