Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. run a sh shell while clicking button in QML with QT5.15.0
Forum Updated to NodeBB v4.3 + New Features

run a sh shell while clicking button in QML with QT5.15.0

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
22 Posts 7 Posters 6.6k Views 4 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.
  • L Offline
    L Offline
    lxy12345
    wrote on last edited by
    #1

    Hi all
    I have google a lot about how to run a shell while clicking button in qml but those topics are too old such like 2013 or 2014 with qquick 1.0. The codes in main.cpp are totally different with those and header files as well.
    Could anyone give me a new demo code about how to run shell?
    Thanks

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      What do you mean by "run shell"? What are you trying to achieve?

      If you want to run an external process, use QProcess. If you want to show a terminal inside your app, it will be harder. You can probably use KDE's frameworks to get a Konsole inside your app, like Dolphin, Yakuake and others do.

      (Z(:^

      1 Reply Last reply
      4
      • L Offline
        L Offline
        lxy12345
        wrote on last edited by
        #3

        Hi, I just want to run an external process which is just a binary file. I write a shell script to run that binary file and want to run this shell while clicking the button in QML.
        Do you have any demo codes?

        mrjjM 1 Reply Last reply
        0
        • L lxy12345

          Hi, I just want to run an external process which is just a binary file. I write a shell script to run that binary file and want to run this shell while clicking the button in QML.
          Do you have any demo codes?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @lxy12345
          Hi
          did you try
          http://71.254.128.33/qml/process.html

          L 1 Reply Last reply
          3
          • mrjjM mrjj

            @lxy12345
            Hi
            did you try
            http://71.254.128.33/qml/process.html

            L Offline
            L Offline
            lxy12345
            wrote on last edited by
            #5

            @mrjj Hi, I followed your post but an error occured.
            I copied the process.h file.
            Here below is my main.cpp:
            #include <QGuiApplication>
            #include <QQuickView>
            #include <QQmlEngine>
            #include <QtQml>
            #include "process.h"

            int main(int argc, char* argv[])
            {
            QGuiApplication app(argc,argv);
            QQuickView view;
            view.setResizeMode(QQuickView::SizeRootObjectToView);
            // Qt.quit() called in embedded .qml by default only emits
            // quit() signal, so do this (optionally use Qt.exit()).
            QObject::connect(view.engine(), &QQmlEngine::quit,
            qApp, &QGuiApplication::quit);
            qmlRegisterType<Process>("Process",1,0,"Process");

            view.setSource(QUrl("qrc:///main.qml"));
            //view.resize(1000, 480);
            view.show();
            return app.exec();
            

            }

            Here below is my qml file:
            Item {
            id: usbcam2
            signal clicked
            x:10
            y:450
            width : 140
            height: 70

                BorderImage {
                    id: buttonImage2
                    source: "images/toolbutton.sci"
                    width: usbcam2.width
                    height: usbcam2.height
                }
                MouseArea {
                    id: mouseRegion2
                    anchors.fill: buttonImage2
                    onClicked: process.start("/bin/cat", [ "/proc/uptime" ]);
                }
                Text {
                    id: btnText2
                    anchors.fill: buttonImage2
                    anchors.margins: 5
                    text: "USBcam2"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                    color: "white"
                    font.bold: true
                    style: Text.Raised
                    styleColor: "black"
                    font.pixelSize: 14
                }
            
            }
            

            }

            When I click the button, the error showed:
            qrc:/main.qml:99: ReferenceError: process is not defined
            what's that meanning?

            ODБOïO 1 Reply Last reply
            0
            • L lxy12345

              @mrjj Hi, I followed your post but an error occured.
              I copied the process.h file.
              Here below is my main.cpp:
              #include <QGuiApplication>
              #include <QQuickView>
              #include <QQmlEngine>
              #include <QtQml>
              #include "process.h"

              int main(int argc, char* argv[])
              {
              QGuiApplication app(argc,argv);
              QQuickView view;
              view.setResizeMode(QQuickView::SizeRootObjectToView);
              // Qt.quit() called in embedded .qml by default only emits
              // quit() signal, so do this (optionally use Qt.exit()).
              QObject::connect(view.engine(), &QQmlEngine::quit,
              qApp, &QGuiApplication::quit);
              qmlRegisterType<Process>("Process",1,0,"Process");

              view.setSource(QUrl("qrc:///main.qml"));
              //view.resize(1000, 480);
              view.show();
              return app.exec();
              

              }

              Here below is my qml file:
              Item {
              id: usbcam2
              signal clicked
              x:10
              y:450
              width : 140
              height: 70

                  BorderImage {
                      id: buttonImage2
                      source: "images/toolbutton.sci"
                      width: usbcam2.width
                      height: usbcam2.height
                  }
                  MouseArea {
                      id: mouseRegion2
                      anchors.fill: buttonImage2
                      onClicked: process.start("/bin/cat", [ "/proc/uptime" ]);
                  }
                  Text {
                      id: btnText2
                      anchors.fill: buttonImage2
                      anchors.margins: 5
                      text: "USBcam2"
                      horizontalAlignment: Text.AlignHCenter
                      verticalAlignment: Text.AlignVCenter
                      elide: Text.ElideRight
                      color: "white"
                      font.bold: true
                      style: Text.Raised
                      styleColor: "black"
                      font.pixelSize: 14
                  }
              
              }
              

              }

              When I click the button, the error showed:
              qrc:/main.qml:99: ReferenceError: process is not defined
              what's that meanning?

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by ODБOï
              #6

              @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

              Here below is my qml file:

              you did not create a Process item

               Process {
                      id: process     
                  }
              
              L 1 Reply Last reply
              5
              • ODБOïO ODБOï

                @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                Here below is my qml file:

                you did not create a Process item

                 Process {
                        id: process     
                    }
                
                L Offline
                L Offline
                lxy12345
                wrote on last edited by
                #7

                @LeLev I don't really understand this code:
                onReadyRead: text.text = readAll();
                Is onReadyRead a signal? or something like onCliked in button?

                jsulmJ 1 Reply Last reply
                0
                • L lxy12345

                  @LeLev I don't really understand this code:
                  onReadyRead: text.text = readAll();
                  Is onReadyRead a signal? or something like onCliked in button?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                  Is onReadyRead a signal?

                  yes

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  L 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                    Is onReadyRead a signal?

                    yes

                    L Offline
                    L Offline
                    lxy12345
                    wrote on last edited by
                    #9

                    @jsulm Hi, jsulm. I have tried your post but I misunderstand somewhere maybe.
                    I don't understand the singal "onReadyRead", I googled which said that this signal is for TCP/IP??? I am not very sure about that.
                    what and where should I modify in your post?

                    mrjjM 1 Reply Last reply
                    0
                    • L lxy12345

                      @jsulm Hi, jsulm. I have tried your post but I misunderstand somewhere maybe.
                      I don't understand the singal "onReadyRead", I googled which said that this signal is for TCP/IP??? I am not very sure about that.
                      what and where should I modify in your post?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                      onReadyRead

                      Hi
                      Its also for other things.
                      " QProcess emits readyRead() when data is available on the current read channel. It also emits readyReadStandardOutput() when new standard output data is available"

                      https://doc.qt.io/qt-5/qprocess.html
                      in the Communicating via Channels section

                      L 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                        onReadyRead

                        Hi
                        Its also for other things.
                        " QProcess emits readyRead() when data is available on the current read channel. It also emits readyReadStandardOutput() when new standard output data is available"

                        https://doc.qt.io/qt-5/qprocess.html
                        in the Communicating via Channels section

                        L Offline
                        L Offline
                        lxy12345
                        wrote on last edited by
                        #11

                        @jsulm when I tried the code ,
                        Text {
                        id: text
                        }

                        Process{
                            id: process
                             onReadyRead: text.text = readAll();
                        }
                        Timer {
                             interval: 10
                             repeat: true
                             triggeredOnStart: true
                             running: true
                             onTriggered: process.start("/bin/cat", [ "/proc/uptime" ]);
                         }
                        

                        it outputs:
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running
                        QProcess::start: Process is already running

                        I think that the shell here is just "cat /proc/uptime", it should output some numbers.
                        Maybe I should read and understand Qprocess class first?

                        JonBJ 1 Reply Last reply
                        0
                        • L lxy12345

                          @jsulm when I tried the code ,
                          Text {
                          id: text
                          }

                          Process{
                              id: process
                               onReadyRead: text.text = readAll();
                          }
                          Timer {
                               interval: 10
                               repeat: true
                               triggeredOnStart: true
                               running: true
                               onTriggered: process.start("/bin/cat", [ "/proc/uptime" ]);
                           }
                          

                          it outputs:
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running
                          QProcess::start: Process is already running

                          I think that the shell here is just "cat /proc/uptime", it should output some numbers.
                          Maybe I should read and understand Qprocess class first?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @lxy12345
                          I don't know anything about QML, but

                          QProcess::start: Process is already running

                          means that you have a QProcess instance where you have set of a start() but it has not yet completed and you have not waited for it, and now you're trying to set off a new start(). You either need to wait for completion of the process instance, or you need to use a new instance of QProcess each time. As I say, i don't know QML how to do that, but that's the cause/reason.

                          1 Reply Last reply
                          2
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Hi
                            Since you set the timer to repeat, it will start a new QProcess each time and
                            10 ms might be too short. try raising it so the other Qprocess gets change to finished.

                            Timer {
                            interval: 10
                            repeat: true

                            JonBJ 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              Hi
                              Since you set the timer to repeat, it will start a new QProcess each time and
                              10 ms might be too short. try raising it so the other Qprocess gets change to finished.

                              Timer {
                              interval: 10
                              repeat: true

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @mrjj
                              Really it ought have a handler for "process finished" signal, to be robust. I don't know how you do that from QML.

                              OP might have a go at the naughty:

                                   onTriggered: process.start("/bin/cat", [ "/proc/uptime" ]); process.waitForFinished();
                              

                              with all the caveats. I don't know if that would work simpler.

                              mrjjM 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @mrjj
                                Really it ought have a handler for "process finished" signal, to be robust. I don't know how you do that from QML.

                                OP might have a go at the naughty:

                                     onTriggered: process.start("/bin/cat", [ "/proc/uptime" ]); process.waitForFinished();
                                

                                with all the caveats. I don't know if that would work simpler.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @JonB
                                Yes i agree as guessing on execution time is bound to fail in many other cases.

                                L 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @JonB
                                  Yes i agree as guessing on execution time is bound to fail in many other cases.

                                  L Offline
                                  L Offline
                                  lxy12345
                                  wrote on last edited by
                                  #16

                                  @mrjj finally, I found the reason. I should add console.log() to show output . The post is correct, thanks guys

                                  JonBJ 1 Reply Last reply
                                  1
                                  • L lxy12345

                                    @mrjj finally, I found the reason. I should add console.log() to show output . The post is correct, thanks guys

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #17

                                    @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                                    The post is correct

                                    What post is correct?

                                    L 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @lxy12345 said in run a sh shell while clicking button in QML with QT5.15.0:

                                      The post is correct

                                      What post is correct?

                                      L Offline
                                      L Offline
                                      lxy12345
                                      wrote on last edited by
                                      #18

                                      @JonB http://71.254.128.33/qml/process.html This post.

                                      JonBJ 1 Reply Last reply
                                      0
                                      • L lxy12345

                                        @JonB http://71.254.128.33/qml/process.html This post.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #19

                                        @lxy12345
                                        Ah, I see. Yes, that looks reasonable. However, it has no code for waiting for the QProcess::finished signal. I admitted earlier I do not know how QML works with regard to instances. But if it does not create new QProcess instances, and if you re-use the one instance before the cat OS command has completed, you will get the QProcess::start: Process is already running error you showed earlier. Since the /bin/cat /proc/uptime completes "quickly" you may get away with it for a while.

                                        You may well be using this as just an example to play with QProcess, I don't know. But be aware that if you are really trying to do /bin/cat /proc/uptime , that is a waste of time! All that does is an OS command to read the contents of the file. You would be better doing this by just opening the file /proc/uptime and reading its content from QML, without any QProcess OS command at all.

                                        L 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @lxy12345
                                          Ah, I see. Yes, that looks reasonable. However, it has no code for waiting for the QProcess::finished signal. I admitted earlier I do not know how QML works with regard to instances. But if it does not create new QProcess instances, and if you re-use the one instance before the cat OS command has completed, you will get the QProcess::start: Process is already running error you showed earlier. Since the /bin/cat /proc/uptime completes "quickly" you may get away with it for a while.

                                          You may well be using this as just an example to play with QProcess, I don't know. But be aware that if you are really trying to do /bin/cat /proc/uptime , that is a waste of time! All that does is an OS command to read the contents of the file. You would be better doing this by just opening the file /proc/uptime and reading its content from QML, without any QProcess OS command at all.

                                          L Offline
                                          L Offline
                                          lxy12345
                                          wrote on last edited by
                                          #20

                                          @JonB thanks for replying me. The "cat /proc/uptime" cmd is just an example. I change the cmd in my project, I use this code to open a shell which controls some peripherals.

                                          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