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. text append waits until hcitoo scan is finished
Forum Updated to NodeBB v4.3 + New Features

text append waits until hcitoo scan is finished

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 3.1k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #21

    There's an example in the finished signal documentation using a lambda.
    There's a chapter dedicated to the explanation on how signal and slots work.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • A Anonymous_Banned275

      @SGaist Tep, that is what I am doing and as usual I am stuck on "connect".
      Sure could use a real example of connect to get me going...
      This "wait for completion" sure was of no help to really understand QProcess...

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #22

      @AnneRanch said in text append waits until hcitoo scan is finished:

      Tep, that is what I am doing and as usual I am stuck on "connect".
      Sure could use a real example of connect to get me going...
      This "wait for completion" sure was of no help to really understand QProcess...

      does this help?

      #ifndef HCITOOLPROCESS_H
      #define HCITOOLPROCESS_H
      
      #include <QObject>
      #include <QProcess>
      #include <QElapsedTimer>
      #include <iostream>
      
      class HciToolProcess : public QObject
      {
          Q_OBJECT
      public:
          enum HciCommands : uint8_t {
              Dev,
              Inq,
              Scan,
          };
      
          explicit HciToolProcess(QObject *parent = nullptr) : QObject(parent), m_currentCommand{HciCommands::Dev}
          {
              connect(&m_process, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished), this, &HciToolProcess::nextCommand);
          }
      
          bool startProcess(){
              if(m_currentCommand != HciCommands::Dev)
                  return false;
      
              m_elapsedTime.start();
      
              m_process.start(QStringLiteral("hcitool"), QStringList(HciCommandToString(m_currentCommand)), QIODevice::ReadWrite);
      
              return true;
          }
      
      signals:
          void timeElapsed(const QString &time); // to update your textEdit_2
          void allProcessesDone();
      
      private:
          void nextCommand(int exitCode, QProcess::ExitStatus exitStatus){
              std::cout<<"\n * Program to demonstrate the usage of linux commands in qt * \n";
      
              QString StdOut      =   m_process.readAllStandardOutput();  //Reads standard output
              QString StdError    =   m_process.readAllStandardError();   //Reads standard error
      
              std::cout<<"\n Printing the standard output..........\n";
              std::cout<<std::endl<<StdOut.toStdString();
              std::cout<<"\n Printing the standard error..........\n";
              std::cout<<std::endl<<StdError.toStdString();
              std::cout<<"\n Printing exit code..........\n";
              std::cout<<std::endl<<exitCode;
              std::cout<<"\n Printing exitStatus..........\n";
              std::cout<<std::endl<<exitStatus;
              std::cout<<"\n\n";
      
              QString elapsedTime = QString::number(m_elapsedTime.restart());
              emit timeElapsed(elapsedTime);
      
              m_currentCommand = static_cast<HciCommands>(m_currentCommand + 1);
              if(static_cast<uint8_t>(m_currentCommand) <= static_cast<uint8_t>(HciCommands::Scan)){
                  m_process.start(QStringLiteral("hcitool"), QStringList(HciCommandToString(m_currentCommand)), QIODevice::ReadWrite);
              } else {
                  m_currentCommand = HciCommands::Dev;
                  emit allProcessesDone();
              }
          }
      
          QString HciCommandToString( HciCommands cmd) {
              switch (cmd) {
              case HciCommands::Dev: return QStringLiteral("dev");
              case HciCommands::Inq: return QStringLiteral("inq");
              case HciCommands::Scan: return QStringLiteral("scan");
              default:
                  Q_UNREACHABLE();
                  return QLatin1String();
              }
          }
      
      private:
          QElapsedTimer m_elapsedTime;
          HciCommands m_currentCommand;
          QProcess m_process;
      };
      
      #endif // HCITOOLPROCESS_H
      
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JonBJ 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @AnneRanch said in text append waits until hcitoo scan is finished:

        Tep, that is what I am doing and as usual I am stuck on "connect".
        Sure could use a real example of connect to get me going...
        This "wait for completion" sure was of no help to really understand QProcess...

        does this help?

        #ifndef HCITOOLPROCESS_H
        #define HCITOOLPROCESS_H
        
        #include <QObject>
        #include <QProcess>
        #include <QElapsedTimer>
        #include <iostream>
        
        class HciToolProcess : public QObject
        {
            Q_OBJECT
        public:
            enum HciCommands : uint8_t {
                Dev,
                Inq,
                Scan,
            };
        
            explicit HciToolProcess(QObject *parent = nullptr) : QObject(parent), m_currentCommand{HciCommands::Dev}
            {
                connect(&m_process, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished), this, &HciToolProcess::nextCommand);
            }
        
            bool startProcess(){
                if(m_currentCommand != HciCommands::Dev)
                    return false;
        
                m_elapsedTime.start();
        
                m_process.start(QStringLiteral("hcitool"), QStringList(HciCommandToString(m_currentCommand)), QIODevice::ReadWrite);
        
                return true;
            }
        
        signals:
            void timeElapsed(const QString &time); // to update your textEdit_2
            void allProcessesDone();
        
        private:
            void nextCommand(int exitCode, QProcess::ExitStatus exitStatus){
                std::cout<<"\n * Program to demonstrate the usage of linux commands in qt * \n";
        
                QString StdOut      =   m_process.readAllStandardOutput();  //Reads standard output
                QString StdError    =   m_process.readAllStandardError();   //Reads standard error
        
                std::cout<<"\n Printing the standard output..........\n";
                std::cout<<std::endl<<StdOut.toStdString();
                std::cout<<"\n Printing the standard error..........\n";
                std::cout<<std::endl<<StdError.toStdString();
                std::cout<<"\n Printing exit code..........\n";
                std::cout<<std::endl<<exitCode;
                std::cout<<"\n Printing exitStatus..........\n";
                std::cout<<std::endl<<exitStatus;
                std::cout<<"\n\n";
        
                QString elapsedTime = QString::number(m_elapsedTime.restart());
                emit timeElapsed(elapsedTime);
        
                m_currentCommand = static_cast<HciCommands>(m_currentCommand + 1);
                if(static_cast<uint8_t>(m_currentCommand) <= static_cast<uint8_t>(HciCommands::Scan)){
                    m_process.start(QStringLiteral("hcitool"), QStringList(HciCommandToString(m_currentCommand)), QIODevice::ReadWrite);
                } else {
                    m_currentCommand = HciCommands::Dev;
                    emit allProcessesDone();
                }
            }
        
            QString HciCommandToString( HciCommands cmd) {
                switch (cmd) {
                case HciCommands::Dev: return QStringLiteral("dev");
                case HciCommands::Inq: return QStringLiteral("inq");
                case HciCommands::Scan: return QStringLiteral("scan");
                default:
                    Q_UNREACHABLE();
                    return QLatin1String();
                }
            }
        
        private:
            QElapsedTimer m_elapsedTime;
            HciCommands m_currentCommand;
            QProcess m_process;
        };
        
        #endif // HCITOOLPROCESS_H
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #23

        @J-Hilk
        I was thinking about writing something like this, but assumed the effort would be ignored.

        If you were to get rid of your enumeration/switch statement and instead create some list of the commands (plus parameters) to be used, which the caller passes/sets on the class, it would be a generic "execute any arbitrary list of commands sequentially" class.

        J.HilkJ 1 Reply Last reply
        0
        • JonBJ JonB

          @J-Hilk
          I was thinking about writing something like this, but assumed the effort would be ignored.

          If you were to get rid of your enumeration/switch statement and instead create some list of the commands (plus parameters) to be used, which the caller passes/sets on the class, it would be a generic "execute any arbitrary list of commands sequentially" class.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #24

          @JonB sure, here ya go :D

          #ifndef HCITOOLPROCESS_H
          #define HCITOOLPROCESS_H
          
          #include <QObject>
          #include <QProcess>
          #include <QElapsedTimer>
          #include <iostream>
          
          class HciToolProcess : public QObject
          {
              Q_OBJECT
          public:
          
              struct CommandAndArguments{
                  QString command;
                  QStringList arguments;
              };
          
              explicit HciToolProcess(QObject *parent = nullptr) : QObject(parent), m_index{-1}
              {
                  connect(&m_process, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished), this, &HciToolProcess::onProcessFinished);
              }
          
              bool startProcess(QList<CommandAndArguments> commands){
                  if(m_index != -1 || commands.isEmpty())
                      return false;
          
                  m_cmdAndArgs = commands;
          
                  m_elapsedTime.start();
          
                  //m_index is -1 so the function will set it to 0
                  processCommandAndArguments();
          
                  return true;
              }
          
          signals:
              void timeElapsed(const QString &time); // to update your textEdit_2
              void allProcessesDone();
          
          private:
              void processCommandAndArguments(){
                  m_index++;
                  if(m_index < m_cmdAndArgs.size()){
                      m_process.start(m_cmdAndArgs.at(m_index).command, m_cmdAndArgs.at(m_index).arguments, QIODevice::ReadWrite);
                  } else {
                      m_index = -1;
                      emit allProcessesDone();
                  }
              }
          
              void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus){
                  std::cout<<"\n * Program to demonstrate the usage of linux commands in qt * \n";
          
                  QString StdOut      =   m_process.readAllStandardOutput();  //Reads standard output
                  QString StdError    =   m_process.readAllStandardError();   //Reads standard error
          
                  std::cout<<"\n Printing the standard output..........\n";
                  std::cout<<std::endl<<StdOut.toStdString();
                  std::cout<<"\n Printing the standard error..........\n";
                  std::cout<<std::endl<<StdError.toStdString();
                  std::cout<<"\n Printing exit code..........\n";
                  std::cout<<std::endl<<exitCode;
                  std::cout<<"\n Printing exitStatus..........\n";
                  std::cout<<std::endl<<exitStatus;
                  std::cout<<"\n\n";
          
                  QString elapsedTime = QString::number(m_elapsedTime.restart());
                  emit timeElapsed(elapsedTime);
          
                  processCommandAndArguments();
              }
          
          private:
              QElapsedTimer m_elapsedTime;
              QProcess m_process;
              QList<CommandAndArguments> m_cmdAndArgs;
              int32_t m_index;
          };
          
          #endif // HCITOOLPROCESS_H
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3

          • Login

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