Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Opening KDE Konsole from Qt and sending a command
QtWS25 Last Chance

Opening KDE Konsole from Qt and sending a command

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheQtGuy
    wrote on last edited by
    #1

    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;
    p1.startDetached("konsole");
    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:

    @QProcess p1;
    p1.startDetached("konsole");
    p1.waitForStarted();
    p1.write("echo hello\n");
    p1.waitForBytesWritten();
    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)?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RazrFalcon
      wrote on last edited by
      #2

      Maybe you need to start BASH instead of konsole?

      @bash -c "ps -al"@

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

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

        Edit
        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@

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TheQtGuy
          wrote on last edited by
          #4

          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:
          @
          QDBusConnection::systemBus().connect(
          "org.freedesktop.Hal",
          "/org/freedesktop/Hal/Manager",
          "org.freedesktop.Hal.Manager",
          "DeviceRemoved",
          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.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RazrFalcon
            wrote on last edited by
            #5

            You can use: "callWithCallback":http://doc.qt.nokia.com/4.7-snapshot/qdbusconnection.html#callWithCallback

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rich
              wrote on last edited by
              #6

              Why don't you just use something like this?

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved