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:

    @QProcess p1;

    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:

    @QProcess p1;
    p1.write("echo hello\n");

    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)?

  • Maybe you need to start BASH instead of konsole?

    @bash -c "ps -al"@

    @ QProcess process;
    process.start("bash", QStringList()<<"-c"<<"ps -al");

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

    Yes, it turns out Konsole does! I found this:
    qdbus org.kde.konsole /Sessions/$session_num sendText "$command"
    sleep 0.1
    qdbus org.kde.konsole /Sessions/$session_num sendText $'\n'
    sleep 0.1@


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

  • 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:
    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.

  • Why don't you just use something like this?

    konsole --nofork --hold -e 'ls'

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.