Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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_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


  • 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. https://forum.qt.io/topic/81858/qprocess-batch-files



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