How to using Qprocess to do the interactive mode?



  • 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!



  • 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
    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.


  • Moderators

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



  • @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.


  • Moderators

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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.