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. Using QProcess from a QTcpServer based class [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Using QProcess from a QTcpServer based class [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.0k Views 3 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    Hi,
    I am trying to use a QProcess object from a QTcpServer based class, but when I try to detect the moment a process is done, it seems the signal finished() never is called. Here is a little piece of the code I am using:

    void InternetService::directoryContent(const QString &path)
    {
        process = new QProcess(this);
        process->start("/bin/ls", QStringList() << path);
        connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), 
        this, SLOT(printDirectoryContent(int, QProcess::ExitStatus)));
    }
    
    void InternetService::printDirectoryContent(int exitCode, QProcess::ExitStatus exitStatus)
    {
        qDebug() << "Tracing this slot...";
    }
    

    Any suggestion about how to make it work? Thanks!


    Qt Developer

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      if there's some problem string the process QProcess emits the error() signal; you can connect a slot to it to understand what happens

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @mcosta, you should connect everything before calling start.

        Also, you have a "memory leak" here, since every time you call directoryContent, you create new QProcess

        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
        0
        • X Offline
          X Offline
          xtingray
          wrote on last edited by xtingray
          #4

          Thank you!
          Using the error() signal I could trace the problem.

          connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
          

          But, now I have a new question: How can I capture the output of the process in an accurate way? I was trying to use the readLine() method but all I get is a NULL pointer exception:

                  char *data = NULL;
                  int size = process->readLine(data, 4);
          

          Any suggestion? Any small example to understand how to do it correctly? Thanks.


          Qt Developer

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi,

            if you need to read the standard output after the process finished, the simplest way is to use QProcess: readAllStandardOutput()

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0
            • X Offline
              X Offline
              xtingray
              wrote on last edited by
              #6

              It worked perfect! Thanks! :)


              Qt Developer

              1 Reply Last reply
              0
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                Even when the issue seems [SOLVED], if you're using 'ls' as the actual process to run (and not just as a simple example of how to use the QProcess() class) I would like to suggest you please consider using QDir and QFileInfo to deal with directories and files in a portable and platform independant way. See example [1] which may benefit your application in the future.

                [1] http://doc.qt.io/qt-5.4/qdir.html#details

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                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