Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved how to write process?

    General and Desktop
    3
    6
    163
    Loading More Posts
    • 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.
    • F
      filipdns last edited by filipdns

      Helle,

      I want to start process on qt but it's not working.

      with test.bat working code is:

      "C:\Program Files (x86)\LibreOffice 5\program\soffice" --headless --convert-to pdf --outdir "E:\MessagesToConvert" "E:\MessagesToConvert\odv 16 10 2019.doc"
      

      I try to use the code below on main.cpp in my qt project but it's not working, no error but nothing happen:

      program= QDir::fromNativeSeparators(C:/Program Files (x86)/LibreOffice 5/program/soffice")+ " --headless --convert-to pdf --outdir " + QDir::fromNativeSeparators("\"E:\MessagesToConvert\"") +" "+QDir::fromNativeSeparators("\"//E:\MessagesToConvert\odv 16 10 2019.doc+"\"");
             
              myProcess.start(program);
              myProcess.waitForFinished();
      

      What is wrong on this code?

      King regards

      Philippe

      JonB sierdzio 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @filipdns last edited by JonB

        @filipdns
        One thing that's wrong is it won't compile at it's missing a ), so I don't see how you can have tested it, or you're not pasting your code....

        Another thing is that you are using \ in literal C++ strings (e.g. "E:\MessagesToConvert"), which won't be correct when you need to write \\. And you are missing "s. So the code you show cannot be what you have tried, sigh.... Please use copy & paste if you want help for this sort of code.

        Another thing is that you are using QDir::fromNativeSeparators() into a string where Qt won't be translating them back to native separators, so it will send /s in the OS command, which may not work.

        You don't know what's going wrong with your command ("no error but nothing happen") because you have not hooked up QProcess::errorOccurred, nor have you connected so that you can see any messages sent to stdout or stderr, which you should do, then you might get a message.

        Other than those... :)

        Do yourself a favour and do not try to use the QProcess::start(const QString &command, QIODevice::OpenMode mode = ReadWrite) overload, where you are responsible for building the correctly-quoted line. Instead use QProcess::start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode = ReadWrite) overload. Here you pass each argument separately in a list, and leave Qt to figuring out the quoting. And don't use QDir::fromNativeSeparators() in the arguments, as Qt won't know to change them to native separators.

        So in the end you'll have something like (I'm not C++, so syntax may be wrong):

        QString program = "C:/Program Files (x86)/LibreOffice 5/program/soffice");       
        QStringList args = "--headless" << 
                           "--convert-to"
                           "pdf" <<
                           "--outdir" <<
                           QDir::toNativeSeparators("E:/MessagesToConvert") <<
                           QDir::toNativeSeparators("E:/MessagesToConvert/odv 16 10 2019.doc");
               
         myProcess.start(program, args);
        
        
        1 Reply Last reply Reply Quote 6
        • sierdzio
          sierdzio Moderators @filipdns last edited by

          @filipdns said in how to write process?:

          fromNativeSeparators(C:/

          Opening quote is missing before C. Additionally, / is not a native separator on Windows, so fromNativeSeparators() is likely to fail. You don't actually need to convert anything, by the way, Qt handles everything automatically if you just use Unix separator (/).

          You should use QStringList arguments instead of cramming whole invocation into program string.

          (Z(:^

          JonB 1 Reply Last reply Reply Quote 2
          • JonB
            JonB @sierdzio last edited by JonB

            @sierdzio

            Qt handles everything automatically if you just use Unix separator (/).

            Not really. That is true where Qt knows something is a path, e.g. the program parameter to start(). But in the arguments to start()/an OS command it cannot know which might be paths and which might not, so it cannot convert those for you. The arguments need to be passed as you actually want them to come out for the command.

            For example, under Windows in dir /w the w is an option to dir, but in dir \w the \w is a path to dir. You must be careful about how you pass things, e.g. here toNativeSeparators() when you mean the option, or passing /w as an argument where you mean it as a path to convert, would go wrong.... My example code (should hopefully!) indicate the correct requirements for the OP's command.

            1 Reply Last reply Reply Quote 3
            • F
              filipdns last edited by

                      Thank you very much for your help, below the working code:
              
                              QString program = "C:/Affichage/Display/LibreOffice 5/program/soffice" ;
                              QStringList args;
                              args<<"--headless";
                              args<<"--convert-to";
                              args<<"pdf";
                              args<<"--outdir";
                              args<<"C:/Affichage/Display/MessagesToConvert";
                              args<<"C:/Affichage/Display/MessagesToConvert/"+filename;
                              myProcess.start(program, args);
                              myProcess.waitForFinished();
              
              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @filipdns last edited by

                @filipdns
                OK, and that's good, but for anyone else reading this the point about the file paths means that the "correct"/"at least preferable" way would pass those argument paths as:

                args<<QDir::toNativeSeparators("C:/Affichage/Display/MessagesToConvert");
                args<<QDir::toNativeSeparators("C:/Affichage/Display/MessagesToConvert/"+filename);
                
                1 Reply Last reply Reply Quote 4
                • First post
                  Last post