JaGoLi / ytdl-gui

A simple-to-use, cross-platform graphical interface for youtube-dl.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Recommendation] Use QProcess instead of std::system

mhogomchungu opened this issue · comments

QProcess[1] offers the following benefits :-

  1. Its possible to cancel a managed process using its terminate[2] member function.
  2. Its easy to notice the managed process has new data and to read it. This will remove the need to first write managed process output to file and then read the output back to your application from the file.
  3. Its possible to easily run many managed instances by running it asynchronously.

[1] https://doc.qt.io/qt-5/qprocess.html
[2] https://doc.qt.io/qt-5/qprocess.html#terminate

An example use of QProcess that starts a process and read its output is :-

    auto exe = new QProcess() ;

    connect( exe,&QProcess::readyReadStandardOutput,[ = ](){
	   /*
	    * Get new data from std out and use it
	    */
	   QByteArray output = exe->readAllStandardOutput() ;
    } ) ;

    connect( exe,&QProcess::readyReadStandardError,[ = ](){
	   /*
	    * Get new data from std error and use it
	    */
	   QByteArray output = exe->readAllStandardError() ;
    } ) ;

    auto s = static_cast< void( QProcess::* )( int,QProcess::ExitStatus ) >( &QProcess::finished ) ;

    connect( exe,s,[ = ]( int e,QProcess::ExitStatus s ){
	    /*
	     * process exit status of the managed process
	     */
	    std::cout<< (int)s;
	    /*
	     * delete qprocess instance
	     */
	    exe->deleteLater() ;
    } ) ;

    exe->start( cmd,arguments ) ;

I added basic qprocess support to beta branch. Even though not all features mentioned here are implemented, it does now allow to properly kill the process instead of using a generic killall command. It is a bit difficult to migrate to QByteArrays without causing breaks. Also, I tried passing arguments to youtubedl directly in a QStringList, but it seems like some of the more complex arguments require a shell for them to be processed properly by youtubedl.

Can you give an example of a complex argument?

I was having issues with entering urls delineated by quotes, and the grep command with string literals being converted to qstring. I think I can solve them with more time and a refactoring of the way arguments are passed to youtube-dl, as well as changing the way the progress bar is updated by using a QByteArray described here.