Qt5.4.1 Windows7 QProcess [SOLVED]



  • Hello everyone.
    I am trying to start the pdf viewer (adobe for windows and evince for linux) from within my program.
    The problem is that on Linux works while on windows does not start, or better, the viewer does not appear.
    This below is my code:

    *void FormMetodi::on_pushButton_4_clicked()
    {
    QString program ;
    #ifdef Q_OS_LINUX
    program = "evince";
    #endif
    #ifdef Q_OS_WIN
    program = "acrord32.exe";
    #endif

     QStringList Parameters;
     Parameters<<ui->tableMetodi->item(ui->tableMetodi->currentRow(),3)->text();
    
     QProcess *myProcess = new QProcess();
     // Start the QProcess instance.
    
     myProcess->start(program,Parameters);
    
     if(myProcess->waitForStarted()){
         qDebug() << "Starting";
     }
    
     if(myProcess->Running){
         qDebug() << "Running";
     }
    
     myProcess->waitForFinished(-1);
     myProcess->close();
    
     qDebug() << myProcess->isOpen();
     qDebug() << myProcess->ReadError;
     qDebug() << program;
     qDebug() << Parameters;
    

    }

    The debug outpt is :
    Running
    false
    3
    "acrord32.exe"
    ("C:/Users/franaria/Desktop/integrativo-2014-1.pdf")

    On Windows, I also tried QDesktopServices but the problem here is that if there are spaces in the path, or the path refers to a network drive does not work.

    QDesktopServices::openUrl(QUrl(ui->tableMetodi->item(ui->tableMetodi->currentRow(),3)->text(), QUrl::TolerantMode));

    What is the best method to launch an external application? And above all, where am I doing wrong?
    Thanks to all


  • Moderators

    QProcess will only work like that if "acrord32.exe" is in a system discoverable location e.g. PATH environment variable or one of the system directories. That is almost never the case on users machines.

    QDesktopServices should work in this case fine. It can also handle spaces. Can you give an example of a path that doesn't work?



  • It might be better to use the default application to open the file.

    In OS X you use the open command (i.e. open name_of_file.pdf). In GNU/Linux the command is 'xdg-open' and on Windows it would be 'start' (?). I have also read that in Windows you can simply pass the name of the file to run and if it is a non executable it will automatically open with the default application (I don't know if this is true or what conditions are required). The Windows one might require you open a console (cmd.exe) with the start command. Don't know about the Windows version of this but I am sure it can be done.



  • Hi

    SOLVED

    Instead of using:
    QDesktopServices::openUrl(QUrl(ui->tableMetodi->item(ui->tableMetodi->currentRow(),3)->text(), QUrl::TolerantMode));

    I used this
    QDesktopServices::openUrl(QUrl::fromLocalFile(ui->tableMetodi->item(ui->tableMetodi->currentRow(),3)->text()));

    The difference is in QUrl::fromLocalFile

    In this way I can open the pdf from both local disk that shared disk and I have no more problems with spaces in the name of the file or directory.
    This evening I will try also on Linux so I will be able to tell you if I get the same behavior.

    Thank you all.

    In any case in Windows I was not able to launch an application with gui using qprocess. :-(


  • Moderators

    @panoramix said:

    In any case in Windows I was not able to launch an application with gui using qprocess. :-(

    That's because of what I said. Either the app needs to be in the PATH/system directory or you need to specify the full path e.g. "C:/Program Files (x86)/Adobe/Reader 11.0/Reader/AcroRd32.exe". Of course the full path is impractical and messing with the PATH is plain evil so QDesktopServices is definitely the "right thing to do" and the way to go.



  • It also works well on Linux.
    Thanks Chris Kawa I'll try using the full path.
    Good weekend to everyone


  • Moderators

    No. I just said you shouldn't! Not all users have it installed in that path and not all users even have drive C:.
    Stick with the QDesktopServices.



  • @Chris-Kawa said:

    No. I just said you shouldn't! Not all users have it installed in that path and not all users even have drive C:.
    Stick with the QDesktopServices.

    I understand that isn't the correct approach but I want to do a test with QProcess to learn to use it.
    In my program I will use , obviously , QDesktopServices :: OpenURL.
    Again thanks for the help Chris-Kawa


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.