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. Running a bash script and outputting output to text box
Forum Updated to NodeBB v4.3 + New Features

Running a bash script and outputting output to text box

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 4.6k Views 1 Watching
  • 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.
  • U Offline
    U Offline
    udev01
    wrote on last edited by
    #1

    Hello,

    I wanted to know if it's possible to run a bash script in the background and read it's output into a text box?
    UPDATE: So far, i have used QProcess to run the command "w" and log it's output via QDebug. How can i now send this to my text box in qml?

    @QProcess proc;
    proc.start("/bin/sh");
    proc.waitForStarted();
    Q_FOREACH( QString cmd, QStringList() << "w" )
    {
    proc.write((cmd + "\n").toLocal8Bit());
    proc.waitForReadyRead();
    qDebug() << proc.readAllStandardOutput().split('\n');
    }
    proc.write("exit\n");
    proc.waitForFinished();
    @

    Thanks.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      You can expose a C++ functions to QML. Just Wrap the above code into a C++ class. Then using properties, methods and signals you can send the output to QML side.
      Please go through this "doc":http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html carefully.

      Also waitFor* functions are blocking. Try using "readyReadStandardOutput()":http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput instead.

      Edited

      157

      1 Reply Last reply
      0
      • U Offline
        U Offline
        udev01
        wrote on last edited by
        #3

        Thanks. I've tried this with QObject and here's what i have:

        http://pastebin.com/kgvSS1BN

        The text is not updating...

        1 Reply Last reply
        0
        • U Offline
          U Offline
          udev01
          wrote on last edited by
          #4

          Oddly enough, this works (just a different main.qml with a new project):
          @import QtQuick 2.0

          Item {
          id: item1
          width: 400
          height: 400
          property alias text1Text: text1.text
          
          Text {
              id: text1
              width: 400
              height: 29
              color: "red"
              text: "This text should change..."
              font.pixelSize: 12
          }
          
          }
          

          @

          Can you tell me why this works and the other one with a different design doesn't?

          Thanks!

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @
            QObject *rootObject = view.rootObject();
            rootObject->setProperty("text1Text",QVariant("Casdasdasda."));
            @

            With earlier QML it didnt work because there's no such text1Text in rootObject which was MainView

            And with the second QML, rootObject Item has this property.

            To make it work with earlier QML, you can use use findChild to find the exact element by objectName.
            You can find an example "here":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name.

            157

            1 Reply Last reply
            0
            • U Offline
              U Offline
              udev01
              wrote on last edited by
              #6

              Thanks, solved. One more thing, I've defined object as view.rootObject(); If i use this in my own class for a function, do i have to redefine view like this? It's not recognized in a class other than main.cpp.

              @QQuickView view@

              For anyone looking for a solution, here's what i did to resolve it:

              @ QQuickItem* object = view.rootObject();

                  QObject *text1 = object->findChild<QObject*>("text1");
              
                  if (text1)
                      text1->setProperty("text", "new text");
              

              @

              EDIT: Also, i would like to append each line from readAllStandardOutput() to the text Area, instead of just updating it's current text. The function i'm going for is like a log, it needs to display all output in the text area.

              [quote author="p3c0" date="1421124837"]@
              QObject *rootObject = view.rootObject();
              rootObject->setProperty("text1Text",QVariant("Casdasdasda."));
              @

              With earlier QML it didnt work because there's no such text1Text in rootObject which was MainView

              And with the second QML, rootObject Item has this property.

              To make it work with earlier QML, you can use use findChild to find the exact element by objectName.
              You can find an example "here":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name.[/quote]

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                bq. One more thing, I’ve defined object as view.rootObject(); If i use this in my own class for a function, do i have to redefine view like this?

                Not needed. You can just pass this object as parameter to that class.

                bq. Also, i would like to append each line from readAllStandardOutput() to the text Area, instead of just updating it’s current text. The function i’m going for is like a log, it needs to display all output in the text area.

                You can just emit a signal with its argument as the new outputted line and then use "append()":http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#append-method of TextArea to append it.

                157

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  udev01
                  wrote on last edited by
                  #8

                  [quote author="p3c0" date="1421150392"]bq. One more thing, I’ve defined object as view.rootObject(); If i use this in my own class for a function, do i have to redefine view like this?

                  Not needed. You can just pass this object as parameter to that class.

                  ^ Can you give me an example of how to do this? Thanks.

                  bq. Also, i would like to append each line from readAllStandardOutput() to the text Area, instead of just updating it’s current text. The function i’m going for is like a log, it needs to display all output in the text area.

                  You can just emit a signal with its argument as the new outputted line and then use "append()":http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#append-method of TextArea to append it.[/quote]

                  Thanks, got it.

                  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