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. run .bat in Qt application

run .bat in Qt application

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.1k Views 1 Watching
  • 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 Offline
    F Offline
    filipdns
    wrote on last edited by filipdns
    #1

    hello,

    I'm trying to run a .bat (I'm sure it's a working one by double click) with Qprocess but it doesn't work:

    here my .h

    #ifndef IMAGEMAGICK_H
    #define IMAGEMAGICK_H
    
    #include <QObject>
    #include <QProcess>
    
    class imagemagick : public QObject
    {
        Q_OBJECT
    
    public:
    
        Q_INVOKABLE void convertimg();
        QString program;
    
        QProcess myProcess;
    };
    
    #endif // IMAGEMAGICK_H
    

    here my .cpp

    #include "imagemagick.h"
    #include <QProcess>
    
    void imagemagick::convertimg()
    {
        program = "c:/test.bat";
        myProcess.start(program);
        myProcess.waitForFinished();
    }
    
    

    here my main.cpp

    #include <QtWebEngine/qtwebengineglobal.h>
    #include <QQmlContext>
    #include <QApplication>
    #include <QOpenGLContext>
    #include <QtSql>
    #include <QtQml>
    #include <QString>
    #include "imagemagick.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
        QSurfaceFormat format;
        format.setDepthBufferSize(24);
        QSurfaceFormat::setDefaultFormat(format);
        QtWebEngine::initialize();
        QQmlApplicationEngine engine;
        imagemagick img;
        engine.rootContext()->setContextProperty("img",&img);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return app.exec();
    }
    

    here my main.qml

    import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Dialogs 1.1
    import QtWebView 1.1
    import QtWebEngine 1.1
    import QtQuick.Window 2.3
    
    Window {
        id:window
        visible: true
        visibility:"Maximized"
        title: qsTr("Hello World")
    
        Component.onCompleted: img.convertimg()
    }
    

    .bat file

    ::Turn of displaying the code on the screen
    @echo off
    
    :: Read all the pdf images from the directory, convert them and save as a png in a different directory
    for %%f in ("c:/*.pdf") do ( magick convert "c:/*.pdf" "e:/*.jpg" )
    

    What I did wrong?

    thank you for your help

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Apart from the fact that I would call 'magick' directly rather than trough a bat file you have to call cmd.exe which parses the bat file - see e.g. https://forum.qt.io/topic/81858/qprocess-batch-files

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      F 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Apart from the fact that I would call 'magick' directly rather than trough a bat file you have to call cmd.exe which parses the bat file - see e.g. https://forum.qt.io/topic/81858/qprocess-batch-files

        F Offline
        F Offline
        filipdns
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        in fact, the bat is not working, it's my previous version was, then, now it's working to call imagemagick like I did

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then please mark the thread as solved, thx.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • F Offline
            F Offline
            filipdns
            wrote on last edited by
            #5

            Yes I will, last question in case you know why,

            the bash command:

            magick convert "c:/test.pdf" "e:/test.jpg"
            

            but the command:

            for %%f in ("c:/*.pdf") do ( magick convert "c:/*.pdf" "e:/*.jpg" )
            

            don't, could you help me?

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I don't know bash that good to answer this question but as I said earlier I don't see any reason why you need a batch for here at all. Qt has all the features to get the content of a directory...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • F Offline
                F Offline
                filipdns
                wrote on last edited by filipdns
                #7

                it's now working, imagemagick need file name for the output like:

                for %%f in ("c:/*.pdf") do ( magick convert "c:/*.pdf" "e:/output.jpg" )
                

                But you have right, I have to look for qt features to see how to get the content of directory, I never use it.

                thank you for your help

                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