Results 1 to 5 of 5

Thread: Communicating with program for GUI input

  1. #1
    Join Date
    Feb 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Communicating with program for GUI input

    Hello,

    My question is in regards to if it is possible for QT to access a variable in a C++ program that is not included in the QTCreator IDE. The external project outputs a value to a text file. Reading from this text file and then using that value in the GUI is inefficient. My goal is to use QT to run a shell script to start this program. Then I would like to know if there is some way to incorporate the code containing this variable directly into the GUI. I was looking into using QProcess but saw this was for input and output streams. I am new to programming and this seemed rather complex. Is there a way to just read this value from the variable and use that value for the GUI or is there a way to use QProcess to do so and if so can you explain how that may work?
    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Communicating with program for GUI input

    As you say and as can be deduced from how you formulated your question, you are new to programming.
    The task you want to achieve is by no means a beginner task.
    What you are referring to as a use case for an IPC (Inter process communication) and there are various possibilities, which one to use depends on your needs.
    No one of there options is simple for a beginner as you need to be familiar with various programming concepts.
    A good place to start would be here:
    https://en.wikipedia.org/wiki/Inter-..._communication
    In addition there is DBus, and a Qt implementation of it QDBus:http://doc.qt.io/qt-5/qtdbus-index.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,010
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Communicating with program for GUI input

    if it is possible for QT to access a variable in a C++ program that is not included in the QTCreator IDE.
    Do you have the source code to this C++ program? If you do, you might be able to adapt it so that it runs as part of a Qt GUI-based program. If not, the simplest option for you is probably to just read from the file after the external program has updated it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Feb 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Communicating with program for GUI input

    The source code is a rather large project, I am unsure as to how to adapt it to a Qt GUI application as I did not write the external project, just the GUI. The way that the external program writes the output that I need to a text file is by redirecting the standard output to a file. Is there any QT Class and functions that you could point me towards that would allow me to read the standard output of the program into the QT GUI without changing the source code that is sending this output to a text file? Any help is greatly appreciated.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,010
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Communicating with program for GUI input

    You might be able to run the external program in a QProcess and redirect its stdout into stdin of your Qt program. See QProcess::setProcessChannelMode()

    Here's some code from a stackoverflow accepted answer to a similar question:

    Qt Code:
    1. p.start( /* whatever your command is, see the doc for param types */ );
    2. p.waitForFinished(-1);
    3.  
    4. QString p_stdout = p.readAllStandardOutput();
    To copy to clipboard, switch view to plain text mode 

    This, however, blocks your Qt app while the external process is running. You could also connect slots to QProcess signals (finished(), readyReadStandardOutput) and not use waitForFinished. See this other stackoverflow answer.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to highlight a menu item by program input
    By horbi in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2015, 09:37
  2. Console input without blocking program
    By havoc in forum Newbie
    Replies: 1
    Last Post: 17th August 2013, 14:28
  3. Input breaks my program all of a sudden?
    By getajob92 in forum Newbie
    Replies: 0
    Last Post: 18th April 2012, 16:54
  4. Replies: 4
    Last Post: 24th July 2009, 08:48
  5. Help! Program dead when changing input methed
    By vencent in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2008, 11:55

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.