Nominate our 2022 Qt Champions!

Problems with QProcess

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

  • Lifetime Qt Champion

    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 ?

  • 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"); 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.

  • Moderators

    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.

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

  • Moderators

    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.

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

  • Moderators

    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

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

  • Moderators

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

    QProcess * wget = new QProcess;
    QStringList arguments;
    arguments << "some argument you want to pass to wget" << "another argument" << ...;
    // 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

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

Log in to reply