Results 1 to 2 of 2

Thread: Using QProcess to capture stdout from statically linked library

  1. #1
    Join Date
    Apr 2019
    Location
    Sacramento, CA
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Using QProcess to capture stdout from statically linked library

    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 Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. QT += webenginewidgets
    6.  
    7. TARGET = GUI_stuff
    8. TEMPLATE = app
    9.  
    10. # The following define makes your compiler emit warnings if you use
    11. # any feature of Qt which has been marked as deprecated (the exact warnings
    12. # depend on your compiler). Please consult the documentation of the
    13. # deprecated API in order to know how to port your code away from it.
    14. DEFINES += QT_DEPRECATED_WARNINGS
    15.  
    16.  
    17. SOURCES += \
    18. main.cpp \
    19. mainwindow.cpp
    20.  
    21. HEADERS += \
    22. mainwindow.h
    23.  
    24. FORMS += \
    25. mainwindow.ui
    26.  
    27. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/release/ -lHelloWorld
    28. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/debug/ -lHelloWorld
    29. else:unix: LIBS += -L$$PWD/lib/ -lHelloWorld
    30.  
    31. INCLUDEPATH += $$PWD/.
    32. DEPENDPATH += $$PWD/.
    33.  
    34. win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/release/libHelloWorld.a
    35. else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/debug/libHelloWorld.a
    36. else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/release/HelloWorld.lib
    37. else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/debug/HelloWorld.lib
    38. else:unix: PRE_TARGETDEPS += $$PWD/lib/libHelloWorld.a
    To copy to clipboard, switch view to plain text mode 


    Here is my main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QLabel>
    4. #include <QTextStream>
    5. #include <QFile>
    6. #include <QTextEdit>
    7. #include <QWebEngineView>
    8. #include "lib/helloworld.h"
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication a(argc, argv);
    13.  
    14. HelloWorld hello_world;
    15. hello_world.display();
    16.  
    17. MainWindow w;
    18. w.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. /**
    2.  * @brief MainWindow::on_pushButton_clicked
    3.  * This function is called when the "Greeting" button is clicked.
    4.  * This function starts a child process in which the console output printed in main()
    5.  * of main.cpp is obtained and stored into a variable to display on the GUI.
    6.  */
    7. void MainWindow::on_pushButton_clicked() {
    8. process = new QProcess;
    9. process->start("./GUI_stuff");
    10. process->waitForStarted(1000);
    11. connect(process,SIGNAL(readyRead()), this, SLOT(printLibData()));
    12. }
    13.  
    14. /**
    15.  * @brief MainWindow::printLibData
    16.  * This function is called when the process in function on_pushButton_clicked()
    17.  * reaches the readyRead() step. The process output is stored in a QString variable
    18.  * which is then displayed in the GUI via a QLabel
    19.  */
    20. void MainWindow::printLibData(){
    21. QString proc_buf = process->readLine();
    22. QLabel *greet_label = new QLabel(proc_buf, ui->centralWidget);
    23. greet_label->setIndent(450);
    24. greet_label->show();
    25. process->close();
    26. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,357
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QProcess to capture stdout from statically linked library

    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,
    _

Similar Threads

  1. Statically link QtQuick into statically linked Windows executable?
    By mattmichler in forum Installation and Deployment
    Replies: 0
    Last Post: 10th September 2015, 22:08
  2. Replies: 0
    Last Post: 3rd December 2013, 11:36
  3. Application seems not to be linked statically
    By Boron in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd April 2008, 17:14
  4. QTextStream capture stdout from xsltParseStylesheetFile
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 25th June 2006, 11:24
  5. statically linked executable
    By smalls in forum Qt Programming
    Replies: 8
    Last Post: 11th February 2006, 02:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.