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. How to using Qprocess to do the interactive mode?

How to using Qprocess to do the interactive mode?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.2k 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.
  • V Offline
    V Offline
    victor wang
    wrote on last edited by
    #1

    Hi All,
    I'm using qt5.5 and Linux+QT for my OS system.
    I wondering is that possible to do the interactive mode by using QProcess?

    For example:
    If i running bluetoothctl in my terminal, it will get into the interactive mode and send the other program like this photo.
    picture

    Is there any method to do that?
    Thanks in advanced!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      This should be possible as QProcess is inheriting from QIODevice. You can use Process itself like any other stdin/out. You can associate the stdin/out with QProcess.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      V 1 Reply Last reply
      3
      • dheerendraD dheerendra

        This should be possible as QProcess is inheriting from QIODevice. You can use Process itself like any other stdin/out. You can associate the stdin/out with QProcess.

        V Offline
        V Offline
        victor wang
        wrote on last edited by
        #3

        @dheerendra
        What you mean is I can send or receive the command with stdin and stdout ?
        Actually I'm thinking to do it like this.

        bool ret=false;
        Qprocess *blue =new QProcess();
        blue->setProcessChannelMode(QProcess::MergedChannels);
        blue->start("sh",QStringList()<<"-c"<</usr/bin/bluetoothctl);
        ret=blue->waitForStarted();
        if(!ret)
        {
            qDebug("start error");
        }
        ret=blue->waitForFinished(-1)
        if(!ret)
        {
            qDebug("finished error");
        }
        delete bluetooth;
        

        And I will do the further command when it wait for finished.

        I don't know if it will work or not I have to try it.

        jsulmJ 1 Reply Last reply
        0
        • V victor wang

          @dheerendra
          What you mean is I can send or receive the command with stdin and stdout ?
          Actually I'm thinking to do it like this.

          bool ret=false;
          Qprocess *blue =new QProcess();
          blue->setProcessChannelMode(QProcess::MergedChannels);
          blue->start("sh",QStringList()<<"-c"<</usr/bin/bluetoothctl);
          ret=blue->waitForStarted();
          if(!ret)
          {
              qDebug("start error");
          }
          ret=blue->waitForFinished(-1)
          if(!ret)
          {
              qDebug("finished error");
          }
          delete bluetooth;
          

          And I will do the further command when it wait for finished.

          I don't know if it will work or not I have to try it.

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

          @victor-wang Take a look at the documentation: http://doc.qt.io/qt-5/qprocess.html
          Especially "Communicating via Channels" chapter.

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

          V 1 Reply Last reply
          1
          • jsulmJ jsulm

            @victor-wang Take a look at the documentation: http://doc.qt.io/qt-5/qprocess.html
            Especially "Communicating via Channels" chapter.

            V Offline
            V Offline
            victor wang
            wrote on last edited by
            #5

            @jsulm
            I've found how to do it.
            This is how I designed.

                bool ret = false;
                QString info;
                QProcess *bluetooth=new QProcess();
                bluetooth->setProcessChannelMode(QProcess::MergedChannels);
                bluetooth->start("sh",QStringList()<<"-c"<<"/usr/bin/bluetoothctl");
                ret=bluetooth->waitForFinished(100);
                if(!ret)
                {
                    qDebug("finished error");
                }
                info=bluetooth->readAllStandardOutput();
                qDebug()<<info;
                QByteArray connect("connect 00:58:50:00:6D:35\n");
                QByteArray exit("exit\n");
                bluetooth->write(connect);
                ret=bluetooth->waitForFinished(200);
                info=bluetooth->readAllStandardOutput();
                qDebug()<<info;
                bluetooth->write(exit);
                ret=bluetooth->waitForFinished(100);
            
                if(bluetooth->exitCode())
                    qDebug("exit unormally");
                else
                {
                    qDebug("exit normally");
                }
                delete bluetooth;
            

            This can do exactly what I want.

            jsulmJ 1 Reply Last reply
            2
            • V victor wang

              @jsulm
              I've found how to do it.
              This is how I designed.

                  bool ret = false;
                  QString info;
                  QProcess *bluetooth=new QProcess();
                  bluetooth->setProcessChannelMode(QProcess::MergedChannels);
                  bluetooth->start("sh",QStringList()<<"-c"<<"/usr/bin/bluetoothctl");
                  ret=bluetooth->waitForFinished(100);
                  if(!ret)
                  {
                      qDebug("finished error");
                  }
                  info=bluetooth->readAllStandardOutput();
                  qDebug()<<info;
                  QByteArray connect("connect 00:58:50:00:6D:35\n");
                  QByteArray exit("exit\n");
                  bluetooth->write(connect);
                  ret=bluetooth->waitForFinished(200);
                  info=bluetooth->readAllStandardOutput();
                  qDebug()<<info;
                  bluetooth->write(exit);
                  ret=bluetooth->waitForFinished(100);
              
                  if(bluetooth->exitCode())
                      qDebug("exit unormally");
                  else
                  {
                      qDebug("exit normally");
                  }
                  delete bluetooth;
              

              This can do exactly what I want.

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

              @victor-wang Why do you wait for the process to finish? If it is finished when it does not make any sense to send more commands to it as it is not running anymore.
              You should use readyReadStandardError() and readyReadStandardOutput() instead.

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

              V 1 Reply Last reply
              2
              • jsulmJ jsulm

                @victor-wang Why do you wait for the process to finish? If it is finished when it does not make any sense to send more commands to it as it is not running anymore.
                You should use readyReadStandardError() and readyReadStandardOutput() instead.

                V Offline
                V Offline
                victor wang
                wrote on last edited by victor wang
                #7

                @jsulm
                I think i can do like this.

                {
                    myProcess = new QProcess(parent);
                    myProcess->start("sh",QStringList()<<"-c"<<"/usr/bin/bluetoothctl");
                    connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
                }
                
                void MainWindow::readOutput(){
                   while(myProcess.canReadLine()){
                       qDebug() << myProcess.readLine();
                  }
                }
                

                But I'm afraid that it will cost too much resource in 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