HMI
Running Windows .exe in the Qt application

Walter Prechtl's picture
by Walter Prechtl last updated 12 November 2020

The task was to write a Qt Quick application (GUI) to upload new firmware to a touch controller. The upload software was provided by the manufacturer in a .exe application that loads a .bin file onto the touch controller. I wanted to use the "QProcess" Qt classes for this, which can be used to call and control shell applications. On the Linux side, I had already used this successfully several times – but on Windows it didn't want to work at first.

QProcess::setWorkingDirectory

The "trick" or solution for this was using "setWorkingDirectory". Here is an excerpt from the .h and .cpp files. Please note the line "process->setWorkingDirectory..." in the .cpp file. cmdlauncher.h

#ifndef CMDLAUNCHER_H
#define CMDLAUNCHER_H

#include <QObject>
#include <QtQuick>
#include <QDebug>
#include <QProcess>
#include <QVariant>
#include <QString>
#include <QDir>

class CmdLauncher : public QProcess
{
    Q_OBJECT

public:
    CmdLauncher(QObject *parent = nullptr);

    Q_INVOKABLE void start(const QString &program, const QVariantList &arguments);

    QString application_directory;
};

#endif // CMDLAUNCHER_H
cmdlauncher.cpp
#include "cmdlauncher.h"

CmdLauncher::CmdLauncher(QObject *parent) : QProcess(parent)
{
    process_running = "start";
}

void CmdLauncher::start(const QString &program, const QVariantList &arguments) {
    QStringList args;

    // convert QVariantList from QML to QStringList for QProcess/
    for (int i = 0; i < arguments.length(); i++)
        args << arguments[i].toString();

    // start request or upload process
    QProcess * process = new QProcess();
    process->setWorkingDirectory(application_directory + "/nConsoleTool");
    process->setProcessChannelMode(QProcess::MergedChannels);
    process->start(program, args);
    process->waitForFinished();

    // get values and set states
    QByteArray bytes = process->readAll();
    cmd_output = QString::fromLocal8Bit(bytes);
    emit cmdOutputChanged();
    }
}

main.qml

import QtQuick 2.9
import QtQuick.Window 2.3
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3
import CmdLauncher 1.0

ApplicationWindow {
    id: application_window
    visible: true
    width: 1024
    height: 768
    title: qsTr("Firmware Tool")

    CmdLauncher {
        id: launcher
    }

    Button {
        id: nUpdateFW
        x: 260
        y: 20
        width: 200
        height: 30
        text: "Upload Firmware"

        onClicked: {
            //console.log(launcher.application_directory + "/nConsoleTool/nUpdateFW.exe");
            if (file_path == "") {
                fileMissing.open();
            } else {
                launcher.start(launcher.application_directory + "/nConsoleTool/nUpdateFW.exe", [ file_path, "-ba" ]);
                if (launcher.controller_detected == false) {
                    controllerMissing.open();
                }
            }
        }
    }
}

You might also be interested in:

  • Raspberry Pi 4 autostart Qt application during boot
  • Raspberry Pi 4 RAM disk
  • Rotating a Raspberry Pi 4 Touch Monitor
  • Products
    • Embedded HMI
    • Industrial Monitors
    • PCAP touch screen
    • Impactinator® glass
  • Design
  • Development
  • Manufacturing
  • Industries
embedded hmi HMI

embedded hmi

Professional solutions
We develop and produce high-quality professional embedded HMI systems for demanding customers and applications.
More information

Start your Project now

Our mission

We are specialists for customised embedded HMI solutions. We see ourselves as a system supplier and offer our customers individual, innovative and cost-optimised control units.

Blog

Click here for the blog

Haven't found the right thing yet?

Use the search function to conveniently search our website for your topic.
More than 5000 pages of specialist knowledge are waiting for you.

Imprint

  • Choose other language:
  • de
  • dk
  • en
  • fr
  • fi
  • nl
  • no
  • it
  • ru
  • pl
  • br
  • es
  • se
  • tr