Important: Please read the Qt Code of Conduct -

run .bat in Qt application

  • 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_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";

    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[])
        QApplication app(argc, argv);
        QSurfaceFormat format;
        QQmlApplicationEngine engine;
        imagemagick img;
        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 {
        visible: true
        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

  • Lifetime Qt Champion

    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.

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

  • Lifetime Qt Champion

    Then please mark the thread as solved, thx.

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

  • Lifetime Qt Champion

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

  • 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

Log in to reply