Thread: Qt GUI to executable with pipe

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Qt GUI to executable with pipe

    Hi,

    I have created this program which allows users to enter a URL and the program will grab the pages content and strip out all HTML tags so only plain text is left.

    Beside this I have created a GUI in Qt so the user can use an interface between the command line utility easily.

    My question is how do I go about link these together via pipes. I can communicate with the executable via popen(), but this is only a one way pipe so i can not return the information back to the GUI.

    I am a little confused in how the Qt program can utilize the executable program. I basically want the user to be able to type a URL in a text box in the Qt GUI and then click submit which will send the URL to the executable which then processes the URL and returns the content to GUI in a plain text area.

    By the way I am using C and working on Linux.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The best idea here would be to write a .so that contains the core functions for grabbing the page and extracting text. Then your command line app and your GUI app could both use that library, rather than having the GUI app call the command line app in the background.

    Otherwise, if the command line app is interactive, you'll need to thread and fork, using multiple pipes to the subprocess in the fork, and setting up some other form of IPC between the thread and the fork. If that sounds like more of a hassle than it is worth, I agree.

    However, if the command line app were not interactive -- ie, if you just gave it the URL as a parameter at invocation -- then popen() should work fine.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Hi thanks for the reply. I think I am going to have to use fork() and IPC to communicate between the two.

    Quick question, do I do the forking in my Qt mainwindow.cpp?

    Does anyone know any good tutorials or examples which I can look at for this?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    First, lemme repeat again that this method "is more hassle than it is worth". It is going to take you 5-10 times longer than if you just rearrange your command line app to work with an abstracted library (which may be a new thing to you, but it is worth learning, whereas learning to do this is not).

    But, if you insist:

    Quote Originally Posted by gunnerone View Post
    Quick question, do I do the forking in my Qt mainwindow.cpp?
    The first thing you do is try to work this out completely outside the context of your application. I cannot stress the importance of that enough. Once you have a working model, then you can try and work it into the GUI.

    The first version does not have to be threaded, it can just use a fork. However, the two forks must communicate somehow. IMO, the best way to do that is via sockets: the parent fork is the server, the child which runs the subprocess is the client. Since this is a simple one to one relationship, UDP is simpler and tidier for this than TCP.

    If you haven't done socket IPC before, then before work that out using completely separate client and server programs before you try a forked version. There are are other options besides sockets -- eg, you can thread and use mutexes/semaphores. That might provide a workable shortcut in the final version, but I kinda doubt it. You could also use shared mem.

    Hopefully, you are starting to see why I said this is an unnecessary hassle, a poor design choice, and going to take you much longer than any other possible option.

    But, if you still insist, the next model would be threaded, because you have to thread into the GUI. You cannot have blocking events going on via the server if it is part of a GUI -- you could perhaps get around the threads by using a non-blocking socket in the server, and try work that into the logic of the GUI somehow. But before you try to conceptualize that, you need to write and play with a non-blocking server. Anyway, your server thread forks a child which controls the input and output pipes of the sub-process, and connects via a socket to the parent to send and receive data. When I said before there might be a shortcut in here, if you can set up non-blocking pipes in the "server" thread, you could do without the fork and the sockets and just use mutexes with the main (GUI) process. But I am not sure how possible or feasible that is.
    Last edited by MK27; 03-13-2012 at 10:55 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 05:19 PM
  2. pipe
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 09-23-2008, 01:35 AM
  3. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  4. calling an executable from an executable
    By dee in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 01:32 PM
  5. Command line executable not a KDE executable?
    By FillYourBrain in forum Linux Programming
    Replies: 3
    Last Post: 10-03-2003, 12:01 PM