PDA

View Full Version : Using QProcess to capture stdout from statically linked library



earlyriser01
18th April 2019, 17:06
Hi all, so I built a simple HelloWorld static library and I want to use it in a GUI application (I named it GUI_stuff - poor name choosing I know..).
My static library has one function (this whole task is for training purposes) that prints a greeting to the console and returns nothing.
I found one way to use QProcess and the GUI_stuff executable to capture the HelloWorld static library output and redirect it to a QLabel.
However, I've been instructed to "fix" this -- i.e. don't rerun GUI_stuff in a child process and just capture the output from the static library.
I've searched high and low in the Qt forums and stackoverflow and have just tried about every solution - don't really know how to go about this.
I'm rather new to Qt as well.

Here is my .pro file:


QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

QT += webenginewidgets

TARGET = GUI_stuff
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += \
main.cpp \
mainwindow.cpp

HEADERS += \
mainwindow.h

FORMS += \
mainwindow.ui

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/release/ -lHelloWorld
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/debug/ -lHelloWorld
else:unix: LIBS += -L$$PWD/lib/ -lHelloWorld

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/release/libHelloWorld.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/debug/libHelloWorld.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/release/HelloWorld.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/debug/HelloWorld.lib
else:unix: PRE_TARGETDEPS += $$PWD/lib/libHelloWorld.a



Here is my main.cpp


#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QTextStream>
#include <QFile>
#include <QTextEdit>
#include <QWebEngineView>
#include "lib/helloworld.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

HelloWorld hello_world;
hello_world.display();

MainWindow w;
w.show();

return a.exec();
}


Here is my mainwindow.cpp file - note I'm only including the functions where I capture and redirect the stdout as this file is rather large


/**
* @brief MainWindow::on_pushButton_clicked
* This function is called when the "Greeting" button is clicked.
* This function starts a child process in which the console output printed in main()
* of main.cpp is obtained and stored into a variable to display on the GUI.
*/
void MainWindow::on_pushButton_clicked() {
process = new QProcess;
process->start("./GUI_stuff");
process->waitForStarted(1000);
connect(process,SIGNAL(readyRead()), this, SLOT(printLibData()));
}

/**
* @brief MainWindow::printLibData
* This function is called when the process in function on_pushButton_clicked()
* reaches the readyRead() step. The process output is stored in a QString variable
* which is then displayed in the GUI via a QLabel
*/
void MainWindow::printLibData(){
QString proc_buf = process->readLine();
QLabel *greet_label = new QLabel(proc_buf, ui->centralWidget);
greet_label->setIndent(450);
greet_label->show();
process->close();
}


I need a way of capturing the output from hello_world.display() in main and redirect it to a QLabel -- please help as I've been stuck on this problem for a few days now...
By the way, the application itself is called GUI_stuff -- so I'm essentially running another GUI_stuff application to get the console output when running GUI_stuff application itself.
Is there a way to use signals and slots here? Is that the solution? Cuz I tried that as well with not much of a solution.

anda_skoa
19th April 2019, 06:58
I really don't understand what you need the QProcess for.

As you see in your main.cpp you can simply include any header of a static library and then create class instances or call functions as if those were defined as part of the program itself.

So if HelloWorld::display() generates a string that should be displayed, simply make it return that string and use that value to set the text of the label.

Cheers,
_