Important: Please read the Qt Code of Conduct -

Compiling a Qt Creator project programmatically

  • I'm trying to compile a Qt Creator project programmatically. For this I'm using the following code:

    QTemporaryFile temp;
    ofstream pro(QString(temp.path() + "/").toLatin1().data());
    pro << "(some stuff)";
    ofstream cpp(QString(temp.path() + "/main.cpp").toLatin1().data());
    cpp << "(some stuff)"
    ofstream user(QString(temp.path() + "/").toLatin1().data());
    user << "(some stuff)";
    QProcess *qmake = new QProcess;
    qmake->start("\"C:\\Qt\\Qt5.7.0_x86\\5.7\\msvc2015\\bin\\qmake.exe\" \"" + temp.path() + "/\" -r -spec win32-msvc2015");
    QObject::connect(qmake, static_cast<void (QProcess::*)(int)>(&QProcess::finished), [=](int exitCode){
        if(exitCode != 0){
            QMessageBox::critical(window, "Error", "Could not compile. The process qmake.exe exited with code " + QString::number(exitCode));
            QProcess *jom = new QProcess;
            jom->start("\"C:\\Qt\\Qt5.7.0_x86\\Tools\\QtCreator\\bin\\jom.exe\" \"" + temp.path() + "/build-project-Desktop_Qt_5_7_0_MSVC2015_32bit-Release\"");
            QObject::connect(jom, static_cast<void (QProcess::*)(int)>(&QProcess::finished), [=](int exitCode){
                if(exitCode == 0){
                    QMessageBox::information(window, "Success", "The program was compiled successfully.");
                    QMessageBox::critical(window, "Error", "Could not compile. The process jom.exe exited with code " + QString::number(exitCode));

    The problem is that the program doesn't compile because jom.exe exits with code 2. But when I open the project in Qt Creator and compile it manually, it works just fine. What am I doing wrong?

  • Hi! Two guesses: Wrong working directory, missing environment variables.

Log in to reply