Results 1 to 13 of 13

Thread: Calling external programs?

  1. #1
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Calling external programs?

    Hi,

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

    Qt Code:
    1. class mainwindow : public QMainWindow, public Ui::MainWindow{
    2. Q_OBJECT
    3.  
    4. private:
    5. [...]
    6. QProcess* proc;
    7. [...]
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void mainwindow::doit(){
    2. QString program="/usr/bin/echo";
    3. QStringList arguments;
    4. arguments << "test";
    5. proc=new QProcess(parent);
    6.  
    7. connect(proc, SIGNAL(readyReadStdout()), this, SLOT(readFromStdout()));
    8. if(!proc->start(program, arguments)){
    9. QMessageBox::critical(0,"fatal error", "could not start", "quit");
    10. exit(-1);
    11. }
    12. }
    13.  
    14. void mainwindow::readFromStdout(){
    15. textEdit->append(???);
    16. }
    To copy to clipboard, switch view to plain text mode 

    First, the "start" does not work:

    Qt Code:
    1. mainwindow.cpp:29: error: no matching function for call to ‘QProcess::QProcess(<unresolved overloaded function type>)’
    To copy to clipboard, switch view to plain text mode 

    Second, what to append to textEdit?

    Can someone help?
    Last edited by Hossie; 17th May 2008 at 15:37.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling external programs?

    Quote Originally Posted by Hossie View Post
    Qt Code:
    1. mainwindow.cpp:29: error: no matching function for call to ‘QProcess::QProcess(<unresolved overloaded function type>)’
    To copy to clipboard, switch view to plain text mode 
    Did you #include <QProcess>?

    Second, what to append to textEdit?
    QProcess::readAllStandardOutput()
    J-P Nurmi

  3. #3
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling external programs?

    mainwindow.cpp:

    Qt Code:
    1. #include <QtGui>
    2. #include <QProcess>
    3. #include "mainwindow.h"
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling external programs?

    Where is variable named "parent" declared? Did you mean "this" or "parent()"?
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    Hossie (17th May 2008)

  6. #5
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling external programs?

    That part was just a paste. I looked at the constructor again, and it seems all arguments are optional. If I change it to

    Qt Code:
    1. proc=new QProcess();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. mainwindow.cpp: In member function ‘void mainwindow::doit()’:
    2. 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’
    To copy to clipboard, switch view to plain text mode 



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

    Qt Code:
    1. Object::connect: No such signal QProcess::readyReadStdout()
    2. Object::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling external programs?

    Quote Originally Posted by Hossie View Post
    That part was just a paste. I looked at the constructor again, and it seems all arguments are optional. If I change it to

    Qt Code:
    1. proc=new QProcess();
    To copy to clipboard, switch view to plain text mode 

    At least that error disappeared.
    Every QObject (QProcess is a QObject) takes a parent parameter for a reason. 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.

    Qt Code:
    1. mainwindow.cpp: In member function ‘void mainwindow::doit()’:
    2. 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’
    To copy to clipboard, switch view to plain text mode 
    Take a closer look at QProcess::start() docs. It's a void function. No bool is returned.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    Hossie (17th May 2008)

  9. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling external programs?

    Quote Originally Posted by Hossie View Post
    €: Without the if part, it compiles. But it does not do what expected:

    Qt Code:
    1. Object::connect: No such signal QProcess::readyReadStdout()
    2. Object::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 
    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.
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    Hossie (17th May 2008)

  11. #8
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling external programs?

    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?

  12. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling external programs?

    Quote Originally Posted by Hossie View Post
    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. 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.
    J-P Nurmi

  13. The following user says thank you to jpn for this useful post:

    Hossie (17th May 2008)

  14. #10
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling external programs?

    Ok, now I think everything works. Thank you!!

  15. #11
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling external programs?

    €: Nevermind. It's working.

  16. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling external programs?

    What does QProcess::waitForFinished() return? If it returns false, what does QProcess::error() return?
    J-P Nurmi

  17. #13
    Join Date
    May 2008
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling external programs?

    Ok you answered before I edited. You are too fast.

    I forgot the proc->start()

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. link error for visual studio.net 2003
    By berlin in forum Newbie
    Replies: 9
    Last Post: 29th September 2006, 16:06
  3. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  4. Can't compile programs in Visual Studio.net 2005
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2006, 14:10

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.