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. Problems with QProcess

Problems with QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 4.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.
  • M Offline
    M Offline
    Maser
    wrote on last edited by Maser
    #1

    Hi.
    Sorry, if i doublepost this question, just point me to the solution, i didnt find any.

    So to my problem. I made a simple application in which the user can chose an ID for a IPTV(InternetTelevision) and then press a start button. This button should start a programm(it downloads the tv-stream) that run in a console and doesnt give it free. There are reasons that this programm occupies the console, so i use QProcess to detach this process so it doesnt bother me and let the GUI accessible. Now at some point when the user wants to change the channel, i have to kill the running process and start it with a different channel_id.
    So now im strugling with the killing part. At the moment i create and start the new process in my void on_StartButton_clicked(); in my qmainwindow , but somehow i cant access it from the StopButton, to terminate it. Do i have to call and create the new process some other way oder do i need a different approach? Any suggestions would be helpful.

    PS: If its important, im using Qt 5.5 on win7.

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

      Hi and welcome to devnet,

      Are you really required to kill the application ? Can't you send it a command ?

      In any case, can you show the code you are using with 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
      • M Offline
        M Offline
        Maser
        wrote on last edited by Maser
        #3

        I can, but i donk know if its going to be helpful. I try to describe what i am doing in words first, and when its not enough, ill post the code.

        So the main task i want to achieve is grabing a videostream from the server and save it. I do it with WGET ( a commandline downloadmanager). I manage to do so by login to server, getting cookies, then asking for a streamlink i want to watch and grabbing it. The stream is a live-TV program. The stream can abruptly end and i have no means to pick it up where it ends. So what i do now i write something like

        system("wget blablabla to file.ts.001");
        system("wget blablabla to file.ts.002");
        ...so like 3-4 more times...
        (this i do in my separate c++prog.exe and then starting this program in Qt on click)
        Im starting WGET in a console-occupying manner so when the stream breaks, it automatically pick it up shortly after by going to ne next system() command. I watch the stream already while its being downloaded and the VLC-Player does play that without interruption and even allow me to to navigate throughout the stream even its separated in several files.

        Since the program is mainly for privat use, i thought this would be ok and i just need to start and kill this program as separated process by click in my QtGUI-App.

        Now i actually wanted to write all i do in my c++prog.exe inside a Qt function(or class). For example i can start WGET in background from the commandline arguments. It spits out a processID that it have in windows and start as a separate process. Problem is, i dont know when the process is going to end, so i have to start another one to pick up stream. I could of course let my GUI-App asking windows frequently if processID of my wget is still running, but ithoughti just could start and stop the exe with a click in my app.

        kshegunovK 1 Reply Last reply
        0
        • M Maser

          I can, but i donk know if its going to be helpful. I try to describe what i am doing in words first, and when its not enough, ill post the code.

          So the main task i want to achieve is grabing a videostream from the server and save it. I do it with WGET ( a commandline downloadmanager). I manage to do so by login to server, getting cookies, then asking for a streamlink i want to watch and grabbing it. The stream is a live-TV program. The stream can abruptly end and i have no means to pick it up where it ends. So what i do now i write something like

          system("wget blablabla to file.ts.001");
          system("wget blablabla to file.ts.002");
          ...so like 3-4 more times...
          (this i do in my separate c++prog.exe and then starting this program in Qt on click)
          Im starting WGET in a console-occupying manner so when the stream breaks, it automatically pick it up shortly after by going to ne next system() command. I watch the stream already while its being downloaded and the VLC-Player does play that without interruption and even allow me to to navigate throughout the stream even its separated in several files.

          Since the program is mainly for privat use, i thought this would be ok and i just need to start and kill this program as separated process by click in my QtGUI-App.

          Now i actually wanted to write all i do in my c++prog.exe inside a Qt function(or class). For example i can start WGET in background from the commandline arguments. It spits out a processID that it have in windows and start as a separate process. Problem is, i dont know when the process is going to end, so i have to start another one to pick up stream. I could of course let my GUI-App asking windows frequently if processID of my wget is still running, but ithoughti just could start and stop the exe with a click in my app.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @Maser
          Hello,
          QProcess::waitForFinished might be what you're after. Although I suggest capturing the stream directly from your Qt program instead of relying on an external program. Here's a simple example.

          Read and abide by the Qt Code of Conduct

          M 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @Maser
            Hello,
            QProcess::waitForFinished might be what you're after. Although I suggest capturing the stream directly from your Qt program instead of relying on an external program. Here's a simple example.

            M Offline
            M Offline
            Maser
            wrote on last edited by
            #5

            @kshegunov
            Hi.
            See, my problem is, id have to make a thread and within the thread a new(oder several new) process for every calling of WGET. The thread would then be waiting for every WGET fo finish. But i need to kill the thread instantly, when im going to change the channel. Can i do that? QTread man says i can cancel a loop in a thread, and if there is no loop, it wont do nothing.

            kshegunovK 1 Reply Last reply
            0
            • M Maser

              @kshegunov
              Hi.
              See, my problem is, id have to make a thread and within the thread a new(oder several new) process for every calling of WGET. The thread would then be waiting for every WGET fo finish. But i need to kill the thread instantly, when im going to change the channel. Can i do that? QTread man says i can cancel a loop in a thread, and if there is no loop, it wont do nothing.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Maser
              I'm not sure why you think a separate thread is needed ... You can kill a thread but this is a very, very bad practice. If you don't want to block your GUI you could start the process (the QProcess class) and then subscribe to it's finished signal, and not create any threads at all.

              Read and abide by the Qt Code of Conduct

              M 1 Reply Last reply
              0
              • kshegunovK kshegunov

                @Maser
                I'm not sure why you think a separate thread is needed ... You can kill a thread but this is a very, very bad practice. If you don't want to block your GUI you could start the process (the QProcess class) and then subscribe to it's finished signal, and not create any threads at all.

                M Offline
                M Offline
                Maser
                wrote on last edited by
                #7

                @kshegunov
                Well, because i do need to kill the running WGET when i want to go to another channel. Actually that was my initial question, how i can start a detached process and then kill it instantly(or almost instantly) on click.

                kshegunovK 1 Reply Last reply
                0
                • M Maser

                  @kshegunov
                  Well, because i do need to kill the running WGET when i want to go to another channel. Actually that was my initial question, how i can start a detached process and then kill it instantly(or almost instantly) on click.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @Maser
                  You use either QProcess::kill or QProcess::terminate, as for starting you do that by QProcess::start. When using QProcess processes by default are started as detached

                  Read and abide by the Qt Code of Conduct

                  M 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @Maser
                    You use either QProcess::kill or QProcess::terminate, as for starting you do that by QProcess::start. When using QProcess processes by default are started as detached

                    M Offline
                    M Offline
                    Maser
                    wrote on last edited by Maser
                    #9

                    @kshegunov
                    "You use either QProcess::kill or QProcess::terminate"
                    Yes, i know. But i dont know how to get it running. Can you draft me an example how can i start a process by clicking on a StartButton and kill/terminate it on StopButton?

                    kshegunovK 1 Reply Last reply
                    0
                    • M Maser

                      @kshegunov
                      "You use either QProcess::kill or QProcess::terminate"
                      Yes, i know. But i dont know how to get it running. Can you draft me an example how can i start a process by clicking on a StartButton and kill/terminate it on StopButton?

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10

                      @Maser
                      This should suffice, assuming your buttons are available as objects:

                      QProcess * wget = new QProcess;
                      wget->setProgram("wget");
                      
                      QStringList arguments;
                      arguments << "some argument you want to pass to wget" << "another argument" << ...;
                      wget->setArguments(arguments);
                      
                      // Connect your stop button to stop the process
                      QObject::connect(stopButton, SIGNAL(clicked()), wget, SLOT(terminate()));
                      // Connect to clean up the QProcess instance when the process has finished
                      QObject::connect(wget, SIGNAL(finished(int, QProcess::ExitStatus)), wget, SLOT(deleteLater()));
                      
                      // Start the process
                      wget->start();

                      Read and abide by the Qt Code of Conduct

                      M 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

                        @Maser
                        This should suffice, assuming your buttons are available as objects:

                        QProcess * wget = new QProcess;
                        wget->setProgram("wget");
                        
                        QStringList arguments;
                        arguments << "some argument you want to pass to wget" << "another argument" << ...;
                        wget->setArguments(arguments);
                        
                        // Connect your stop button to stop the process
                        QObject::connect(stopButton, SIGNAL(clicked()), wget, SLOT(terminate()));
                        // Connect to clean up the QProcess instance when the process has finished
                        QObject::connect(wget, SIGNAL(finished(int, QProcess::ExitStatus)), wget, SLOT(deleteLater()));
                        
                        // Start the process
                        wget->start();
                        M Offline
                        M Offline
                        Maser
                        wrote on last edited by Maser
                        #11

                        @kshegunov
                        Thank you! Ill try it out and then go back to you.

                        PS: Now with your help i am able to terminate my process. Thank you very much!!!

                        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