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. External process from Qml

External process from Qml

Scheduled Pinned Locked Moved Unsolved General and Desktop
34 Posts 4 Posters 13.4k 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.
  • N Offline
    N Offline
    Naveen_D
    wrote on 16 Jan 2017, 09:07 last edited by
    #1

    Hi everyone,
    is it possible to call an external process from qml ?

    Naveen_D

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 16 Jan 2017, 09:09 last edited by
      #2

      @Naveen_D

      is it possible to call an external process from qml ?

      Not directly but you can create a C++ component and expose it to QML and make it execute the external processes

      157

      N 1 Reply Last reply 16 Jan 2017, 09:20
      0
      • P p3c0
        16 Jan 2017, 09:09

        @Naveen_D

        is it possible to call an external process from qml ?

        Not directly but you can create a C++ component and expose it to QML and make it execute the external processes

        N Offline
        N Offline
        Naveen_D
        wrote on 16 Jan 2017, 09:20 last edited by
        #3

        @p3c0 thanks for the reply...
        yea i am doing the same now... i am using a C++ class and Qprocess to start another process....the new process gives me a string (it is a command said in live microphone) which i pass it through signal back to the qml...but what is happening is..when i call the c++ function from qml, the control doesn't wait until the function is over, it continues to print some debug statements, which i have given after that function call

        Naveen_D

        V 1 Reply Last reply 17 Jan 2017, 12:32
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 16 Jan 2017, 09:55 last edited by
          #4

          @Naveen_D Can you post the C++ code ?

          157

          N 1 Reply Last reply 16 Jan 2017, 10:02
          0
          • P p3c0
            16 Jan 2017, 09:55

            @Naveen_D Can you post the C++ code ?

            N Offline
            N Offline
            Naveen_D
            wrote on 16 Jan 2017, 10:02 last edited by
            #5

            @p3c0 ya sure.
            here is the c++ code

            #include "ProcessWidget.h"
            
            ProcessWidget::ProcessWidget(QWidget *parent)
                : QWidget(parent)
            {
            }
            
            void ProcessWidget::vStartProcess()
            {
                qDebug()<<"entered the function"<<endl;
                const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                myProcess = new QProcess;
            
                connect(myProcess,SIGNAL(readyReadStandardError()),this,SLOT(vEndProcess()));
                connect(myProcess,SIGNAL(finished(int)),myProcess,SLOT(deleteLater()));
                myProcess->start(program);
                qDebug()<<"process started waiting for output"<<endl;
                connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);
            
            }
            
            void ProcessWidget::vEndProcess()
            {
                const QString processOut = QString::fromLatin1(myProcess->readAllStandardError());
                qDebug()<<"output>>>>"<<processOut<<endl;
                const QRegularExpression regXp("Received Command:s*(.+)");
                const auto regXpMatch = regXp.match(processOut);
                if(regXpMatch.hasMatch()){
                    qDebug() << "Output of regular exp is: "<< regXpMatch.captured(1);
                    signalData= regXpMatch.captured(1);
                    qDebug()<<"signal data is >>"<<signalData<<endl;
                    emit playmusicsignal(signalData);
                }
            }
            
            
            
            ProcessWidget::~ProcessWidget()
            {
            
            }
            

            Naveen_D

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p3c0
              Moderators
              wrote on 16 Jan 2017, 10:08 last edited by
              #6

              @Naveen_D As per doc:

              Note: Processes are started asynchronously, which means the started() and errorOccurred() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.

              Try static execute instead.

              157

              N 1 Reply Last reply 17 Jan 2017, 04:32
              1
              • P p3c0
                16 Jan 2017, 10:08

                @Naveen_D As per doc:

                Note: Processes are started asynchronously, which means the started() and errorOccurred() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.

                Try static execute instead.

                N Offline
                N Offline
                Naveen_D
                wrote on 17 Jan 2017, 04:32 last edited by
                #7

                @p3c0 Sir i used execute instead of start but...i am not able to pass the output which i got from the new process, back to qml...what may be the problem ?
                my code

                #include "ProcessWidget.h"
                
                ProcessWidget::ProcessWidget(QWidget *parent)
                    : QWidget(parent)
                {
                }
                
                void ProcessWidget::vStartProcess()
                {
                    qDebug()<<"entered the function"<<endl;
                    const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                    myProcess = new QProcess;
                
                    connect(myProcess,SIGNAL(readyReadStandardError()),this,SLOT(vEndProcess()));
                    connect(myProcess,SIGNAL(finished(int)),myProcess,SLOT(deleteLater()));
                    connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);
                    myProcess->execute(program);
                //    myProcess->waitForStarted(30000);
                    qDebug()<<"process started waiting for output"<<endl;
                
                }
                
                void ProcessWidget::vEndProcess()
                {
                    const QString processOut = QString::fromLatin1(myProcess->readAllStandardError());
                    qDebug()<<"output>>>>"<<processOut<<endl;
                    const QRegularExpression regXp("Received Command:s*(.+)");
                    const auto regXpMatch = regXp.match(processOut);
                    if(regXpMatch.hasMatch()){
                        qDebug() << "Output of regular exp is: "<< regXpMatch.captured(1);
                        signalData= regXpMatch.captured(1);
                        qDebug()<<"signal data is >>"<<signalData<<endl;
                        emit playmusicsignal(signalData);
                    }
                }
                
                ProcessWidget::~ProcessWidget()
                {
                
                }
                

                in the above code, i didn't get any debug statements in console from vEndProcess function

                Naveen_D

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 17 Jan 2017, 05:10 last edited by
                  #8

                  @Naveen_D Are you sure you want readyReadStandardError or readAllStandardOutput instead ?

                  157

                  N 1 Reply Last reply 17 Jan 2017, 10:26
                  0
                  • P p3c0
                    17 Jan 2017, 05:10

                    @Naveen_D Are you sure you want readyReadStandardError or readAllStandardOutput instead ?

                    N Offline
                    N Offline
                    Naveen_D
                    wrote on 17 Jan 2017, 10:26 last edited by
                    #9

                    @p3c0

                    Are you sure you want readyReadStandardError or readAllStandardOutput instead ?

                    even when i use readAllStandardOutput i am not able to send the output through signal.

                    Naveen_D

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 17 Jan 2017, 10:29 last edited by
                      #10

                      Maybe I'm being silly here but since you are running what looks like the Hello Speak Example why do you start it as a separate process rather than a separate thread?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      N 1 Reply Last reply 17 Jan 2017, 11:11
                      0
                      • V VRonin
                        17 Jan 2017, 10:29

                        Maybe I'm being silly here but since you are running what looks like the Hello Speak Example why do you start it as a separate process rather than a separate thread?

                        N Offline
                        N Offline
                        Naveen_D
                        wrote on 17 Jan 2017, 11:11 last edited by
                        #11

                        @VRonin Sir i am not aware of threads...i am learning it from begining...for time being i am using separate process to get the required output.

                        Naveen_D

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          VRonin
                          wrote on 17 Jan 2017, 11:29 last edited by
                          #12

                          Try having a look at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          N 1 Reply Last reply 17 Jan 2017, 11:47
                          0
                          • V VRonin
                            17 Jan 2017, 11:29

                            Try having a look at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                            N Offline
                            N Offline
                            Naveen_D
                            wrote on 17 Jan 2017, 11:47 last edited by
                            #13

                            @VRonin thanks for the link sir..i will go through it...but can i know what i need to do with the process thing to get the required output.?

                            Naveen_D

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              VRonin
                              wrote on 17 Jan 2017, 11:57 last edited by
                              #14

                              That's because you block the event loop that you need to read. Use

                              void ProcessWidget::vStartProcess()
                              {
                                  qDebug()<<"entered the function"<<endl;
                                  const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                                  myProcess = new QProcess;
                              QEventLoop blockLoop;
                                  connect(myProcess,SIGNAL(readyReadStandardError()),this,SLOT(vEndProcess()));
                                  connect(myProcess,SIGNAL(finished(int)),myProcess,SLOT(deleteLater()));
                              connect(myProcess,SIGNAL(finished(int)),&blockLoop,SLOT(quit()));
                                  connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);
                                  myProcess->start(program);
                                  qDebug()<<"process started waiting for output"<<endl;
                              blockLoop.exec();
                              }
                              

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              N 1 Reply Last reply 17 Jan 2017, 12:20
                              0
                              • V VRonin
                                17 Jan 2017, 11:57

                                That's because you block the event loop that you need to read. Use

                                void ProcessWidget::vStartProcess()
                                {
                                    qDebug()<<"entered the function"<<endl;
                                    const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                                    myProcess = new QProcess;
                                QEventLoop blockLoop;
                                    connect(myProcess,SIGNAL(readyReadStandardError()),this,SLOT(vEndProcess()));
                                    connect(myProcess,SIGNAL(finished(int)),myProcess,SLOT(deleteLater()));
                                connect(myProcess,SIGNAL(finished(int)),&blockLoop,SLOT(quit()));
                                    connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);
                                    myProcess->start(program);
                                    qDebug()<<"process started waiting for output"<<endl;
                                blockLoop.exec();
                                }
                                
                                N Offline
                                N Offline
                                Naveen_D
                                wrote on 17 Jan 2017, 12:20 last edited by
                                #15

                                @VRonin Sir if i use the EventLoop it is not returning control...it will be in continuous loop like if i give input through live microphone it will wait for next input like that

                                Naveen_D

                                1 Reply Last reply
                                0
                                • N Naveen_D
                                  16 Jan 2017, 09:20

                                  @p3c0 thanks for the reply...
                                  yea i am doing the same now... i am using a C++ class and Qprocess to start another process....the new process gives me a string (it is a command said in live microphone) which i pass it through signal back to the qml...but what is happening is..when i call the c++ function from qml, the control doesn't wait until the function is over, it continues to print some debug statements, which i have given after that function call

                                  V Offline
                                  V Offline
                                  VRonin
                                  wrote on 17 Jan 2017, 12:32 last edited by VRonin
                                  #16

                                  @Naveen_D said in External process from Qml:

                                  but what is happening is..when i call the c++ function from qml, the control doesn't wait until the function is over, it continues to print some debug statements, which i have given after that function call

                                  @Naveen_D said in External process from Qml:

                                  if i use the EventLoop it is not returning control

                                  I'm not sure I understand what you want to achieve. Do you want the process to act synchronously (loop until process is over) or asynchronously (return control almost immediately to the main app)?

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  N 1 Reply Last reply 18 Jan 2017, 04:28
                                  0
                                  • V VRonin
                                    17 Jan 2017, 12:32

                                    @Naveen_D said in External process from Qml:

                                    but what is happening is..when i call the c++ function from qml, the control doesn't wait until the function is over, it continues to print some debug statements, which i have given after that function call

                                    @Naveen_D said in External process from Qml:

                                    if i use the EventLoop it is not returning control

                                    I'm not sure I understand what you want to achieve. Do you want the process to act synchronously (loop until process is over) or asynchronously (return control almost immediately to the main app)?

                                    N Offline
                                    N Offline
                                    Naveen_D
                                    wrote on 18 Jan 2017, 04:28 last edited by
                                    #17

                                    @VRonin the scenario is..when i call the c++ function from qml it should start the process...through which i get the output and will pass the output through a signal back to qml. till the process is not completed it should not return back to main app(i.e Qml app).
                                    Now i am getting the output to my console but i am not able to send it as shown in the above code.

                                    Naveen_D

                                    jsulmJ 1 Reply Last reply 18 Jan 2017, 05:18
                                    0
                                    • N Naveen_D
                                      18 Jan 2017, 04:28

                                      @VRonin the scenario is..when i call the c++ function from qml it should start the process...through which i get the output and will pass the output through a signal back to qml. till the process is not completed it should not return back to main app(i.e Qml app).
                                      Now i am getting the output to my console but i am not able to send it as shown in the above code.

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 18 Jan 2017, 05:18 last edited by
                                      #18

                                      @Naveen_D Why should it not return? What you are trying to do is not how Qt apps should work. You should use signals/slots: you start the process, connect signals/slots and as soon as there is input from the process you handle it in the slot.

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

                                      N 1 Reply Last reply 18 Jan 2017, 05:25
                                      1
                                      • jsulmJ jsulm
                                        18 Jan 2017, 05:18

                                        @Naveen_D Why should it not return? What you are trying to do is not how Qt apps should work. You should use signals/slots: you start the process, connect signals/slots and as soon as there is input from the process you handle it in the slot.

                                        N Offline
                                        N Offline
                                        Naveen_D
                                        wrote on 18 Jan 2017, 05:25 last edited by
                                        #19

                                        @jsulm Sorry sir i didn't get...is the above code wrong which i have posted ? or what changes i need to do ?

                                        One more doubt..how to compare a string in Qml...like in Qt we do (str=="some string")

                                        Naveen_D

                                        jsulmJ 1 Reply Last reply 18 Jan 2017, 06:15
                                        0
                                        • N Naveen_D
                                          18 Jan 2017, 05:25

                                          @jsulm Sorry sir i didn't get...is the above code wrong which i have posted ? or what changes i need to do ?

                                          One more doubt..how to compare a string in Qml...like in Qt we do (str=="some string")

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 18 Jan 2017, 06:15 last edited by
                                          #20

                                          @Naveen_D ProcessWidget::vStartProcess is currently blocking, why? Why not just start the process, connect signals/slots and return from ProcessWidget::vStartProcess ?

                                          void ProcessWidget::vStartProcess()
                                          {
                                              qDebug()<<"entered the function"<<endl;
                                              const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                                              myProcess = new QProcess;
                                              connect(myProcess,SIGNAL(readyReadStandardError()),this,SLOT(vEndProcess()));
                                              connect(myProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(processStandardOutput()));
                                              connect(myProcess,SIGNAL(finished(int)),myProcess,SLOT(deleteLater()));
                                              connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);
                                              myProcess->start(program);
                                          }
                                          

                                          processStandardOutput() will be called as soon as the process is writing something to standard output.

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

                                          N 1 Reply Last reply 18 Jan 2017, 11:08
                                          2

                                          1/34

                                          16 Jan 2017, 09:07

                                          • Login

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