0

I am trying to implement GUI for a simple C++ using Qt to understand how it works. The C++ program and the GUI are in seperate projects in the same solution in VS 2015. The Qt program will call the C++ program using QProcess' start() function. The C++ console application will be running the background will the Qt program acts as an interface. My question is, how I do I pass values to the C++ program using QProcess and How do I obtain the output from the C++ program? Here is the sample program I am working with:-

The C++ Program

#include<iostream>
#include<fstream>

using namespace std;

void main() {
    int a;
    cout << "Enter a number" << endl;
    cin >> a;
    cout << "The Square of the number is " << (a*a) << endl;
        ofstream write;
        write.open("test.txt");
        write << (a * a);
        write.close();
}

The Qt Program

#include "FrontEnd.h" 
#include <QtWidgets/QApplication>
#include <qprocess.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FrontEnd w;
    w.show();

    QProcess p1;
    p1.start("Interface.exe");
    p1.write("5",5);
    return a.exec();
}

I tried to pass the value using the write() function but it did not seem to work as the test.txt file remained empty. The Qt program I have written lacks the GUI features as I will be adding them once I figure out how to send and recieve data using QProcess. Thank you for your assistance!

  • 1
    It's simple! Once the process started (wait for its `started()` signal or `QProcess::waitForStarted()`), write to its `stdin` by `write` method of the process. Then the C++ process gets the data by its `cin` command and returns the result (for simplicity don't write the result on file, return it on `stdout` instead), so in the Qt app you can read the `stdout`. – frogatto Sep 15 '16 at 18:40

1 Answers1

0

You have many problems in the code you are showing:

  1. You are passing write two arguments "5" and 5, this means that it will use 5 chars from the the char* "5". Absolutely, this is not what you want as this will lead to undefined behavior resulted from accessing memory that does not belong to your data.

    Instead you should be using the second version of write which accepts a zero-terminated string, like this: p1.write("5");

  2. In order to make cin in your console program know that the number it should read is finished, you should pass a new line after your number, so your call will end up like this: p1.write("5\n");

  3. You should use the readyRead signal in the Qt program to get notified when your process has new output that you can read, and then you may call readAll from a slot which is connected to that signal.

For completeness, Here is how your code should be:

interface.cpp

#include<iostream>

using namespace std;

void main() {
    int a;
    cin >> a;
    //No need to use file output
    //it is simpler and more appropriate to read the output from stdout
    cout << "The Square of the number is " << (a*a) << endl;
}

The Qt program

#include <QApplication>
#include <QWidget>
#include <QProcess>
#include <QDebug>
#include "FrontEnd.h" 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FrontEnd w;
    w.show();

    QProcess p1;
    p1.start("Interface.exe");
    p1.write("5\n");
    QObject::connect(&p1, &QProcess::readyRead, [&p1](){
        //output to qDebug, you may want to update some GUI component instead
        qDebug() << p1.readAll();
    });
    return a.exec();
}
Mike
  • 8,055
  • 1
  • 30
  • 44