6.7. Executing Other Programs

KDE has the KRun class, a member of the KIO library. It is able to run executables and .desktop files (as produced by KDE).

The most useful members of this class are the static run() functions, shown in Listing 6.5:


Example 6.5. Static run Functions

   1 
   2  1: #include <krun.h>
   3  2:
   4  3: bool run(const KService&_service,
   5  4:          const KURL::List&_urls)
   6  5: bool run(const QString&_exec,
   7  6:          const KURL::List&_urls,
   8  7:          const QString&_name = QString::null,
   9  8:          const QString&_icon = QString::null,
  10  9:          const QString&_mini_icon = QString::null,
  11 10:          const QString&_desktop_file = QString::null)
  12 

The first of these two static run functions allows you to execute services. Those that are available are in your $KDEDIR/share/services. To execute Konqueror, you can use KRun::run(KService(locate("services", "konqueror.desktop")), QStringList());.

This method is, in fact, preferable to just executing Konqueror and hoping that it will work. By filling the QStringList at the end, you can send arguments to Konqueror.

The second static method can be used to execute a program, like so:


   1 
   2 KRun::run("netscape", QStringList("http://www.kde.org"),
   3 "Netscape", locate("icon", "locolor/32x32/apps/netscape.png"));
   4 

It is still, of course, preferable to visit http://www.KDE.org with Konqueror.

You can also open files with KRun. In the next example (see Listings 6.6 and 6.7), when the libraries are ready for their next command, the Open File button becomes re-enabled.


Example 6.6. runwalk.h: Open a File (Header)

   1 
   2  1:
   3  2: #ifndef _RUNWALK_H
   4  3: #define _RUNWALK_H
   5  4:
   6  5: #include <krun.h>
   7  6: #include <qpushbutton.h>
   8  7: #include <klineedit.h>
   9  8: Style Reference
  10  9: class RunWalk : public QWidget
  11 10: {
  12 11:  Q_OBJECT
  13 12:  public:
  14 13:   RunWalk();
  15 14:  public slots:
  16 15:   void slotDoneExec();
  17 16:   void slotRun();
  18 17:  private:
  19 18:   QPushButton *pushme;
  20 19:   KLineEdit *program;
  21 20: };
  22 21:
  23 22: #endif
  24 


Example 6.7. runwalk.cpp: Open a File (Source).XXX

   1 
   2  1: #include <qlayout.h>
   3  2: #include "runwalk.h"
   4  3: #include <klocale.h>
   5  4:
   6  5: RunWalk::RunWalk()
   7  6: {
   8  7:   QVBoxLayout *layout=new QVBoxLayout(this,0,2);
   9  8:   layout->setAutoAdd(true);
  10  9:   program=new KLineEdit("http://www.kde.org/",this);
  11 10:   pushme=new QPushButton(i18n("Open File"),this);
  12 11:   show();
  13 12:
  14 13:   connect(pushme, SIGNAL(clicked()), SLOT(slotRun()));
  15 14: }
  16 15:
  17 16: void RunWalk::slotDoneExec()
  18 17: {
  19 18:   pushme->setEnabled(true);
  20 19:   program->setEnabled(true);
  21 20: }
  22 21:
  23 22: void RunWalk::slotRun()
  24 23: {
  25 24:   KRun *run=new KRun(KURL(program->text()));
  26 25:   connect(run, SIGNAL(finished()), SLOT(slotDoneExec()));
  27 26:   connect(run, SIGNAL(error()), SLOT(slotDoneExec()));
  28 27:   pushme->setEnabled(false);
  29 28:   program->setEnabled(false);
  30 29: }
  31 30:
  32 31: int main(int argc, char **argv)
  33 32: {
  34 33:   KApplication app(argc, argv, "runwalk", true);
  35 34:   RunWalk runwalk;
  36 35:
  37 36:   app.setMainWidget(&runwalk);
  38 37:   return app.exec();
  39 38: }
  40