March 29, 2012

TheQtGuy TheQtGuy
Lab Rat
6 posts

Opening KDE Konsole from Qt and sending a command

 

Ok, so I need to open a KDE linux terminal console from my Qt program, and this is what I’ve done to achieve that:

  1.     p1.startDetached("konsole");
  2.     p1.waitForFinished();

And this works fine.

The problem is: after I open the console (by the way, it has to be konsole, can’t use any “console” function inside Qt), I need to pass a command to it, i.e. I want the console to execute “ps -al” or any other command.

I read a lot about QProcess and all that, and I tried to do the following:

  1.     p1.startDetached("konsole");
  2.     p1.waitForStarted();
  3.     p1.write("echo hello\n");
  4.     p1.waitForBytesWritten();
  5.     p1.waitForFinished();

I even tried to replace startDetached by start since I read that it could be a problem, but again the console that was opened didn’t print anything.

Any ideas how I could open a console and send a command to that console (preferably using p1.startDetached instead of p1.start)?

5 replies

March 29, 2012

RazrFalcon RazrFalcon
Lab Rat
125 posts

Maybe you need to start BASH instead of konsole?

  1. bash -c "ps -al"

  1.     QProcess process;
  2.     process.start("bash", QStringList()<<"-c"<<"ps -al");
  3.     process.waitForFinished();
  4.     qDebug()<<process.readAll();

 Signature 

QT != Qt
Gentoo + KDE

March 29, 2012

Andre Andre
Robot Herder
6397 posts

Not sure, but perhaps Konsole exposes a DBUS interface you can use?

Edit
Yes, it turns out Konsole does! I found this:

  1. qdbus org.kde.konsole /Sessions/$session_num sendText "$command"
  2. sleep 0.1
  3. qdbus org.kde.konsole /Sessions/$session_num sendText $'\n'
  4. sleep 0.1

See http://www.linuxjournal.com/content/start-and-control-konsole-dbus

That means that you should also be able to do this directly from Qt itself, as Qt also supports DBUS directly.

March 30, 2012

TheQtGuy TheQtGuy
Lab Rat
6 posts

I read that but I still have no clue how to do it :(

I did a quick QDBus code in the past, just to receive data from HAL when a device was removed:

  1.     QDBusConnection::systemBus().connect(
  2.                 "org.freedesktop.Hal",
  3.                 "/org/freedesktop/Hal/Manager",
  4.                 "org.freedesktop.Hal.Manager",
  5.                 "DeviceRemoved",
  6.                 this, SLOT(deviceRemoved(const QString)));

But I don’t know how I could make use of that to send a command to a Konsole QApplication.

March 30, 2012

RazrFalcon RazrFalcon
Lab Rat
125 posts

You can use: callWithCallback [doc.qt.nokia.com]

 Signature 

QT != Qt
Gentoo + KDE

April 9, 2012

rich rich
Lab Rat
37 posts

Why don’t you just use something like this?

konsole —nofork —hold -e ‘ls’

 
  ‹‹ [Solved] Windows Port: mingw32 issue (C and C++)      sqlite3 string encoding problem ››

You must log in to post a reply. Not a member yet? Register here!