简体   繁体   中英

Connecting Qt GUI with TCL application

I have a Qt GUI and another application that communicates with the user using TCL interpreter.

I want the Qt and the application to share information created in the application and be shown in GUI. that's why I need them to be threads of same process (share a pointer between them).

Plus, I want the Qt to produce strings that will be sent through the console to the TCL (the application) in the other thread.

How can I do this kind of communication?

My thought is to change the TCL interpreter stdin to be the stdout of the Qt console but I have no idea how to do it!

Create a network socket on the localhost interface in the Tcl application and listen for commands from the Qt application (see socket , fileevent and info complete ). I would suggest that you process the commands received in a slave interpreter ( interp create ) so that you can easily sandbox the commands permitted by the remote application. To pass strings to the Tcl application your Qt app can just send to the socket. To receive data, send a command requesting that data and read the response. If you ensure you only open the listening socket on the localhost interface you can eliminate most security problems. You could look into adding tls if you run into problems or arrange to read a named pipe instead. There are lots of possibly inter-process communications possibilities but typically sockets is the most flexible.

If you're going to capture the Tcl interpreter's standard output (and standard error; some important messages go there!) then you must run the Tcl interpreter in a sub-process, communicating with the outer Qt/C++-based process via pipes. (You should be able to find how to do that in general by searching Stack Overflow…)

Tcl-specific details: You'll really want to make sure that the Tcl interpreter does:

fconfigure stdout -buffering line

This is because in non-interactive mode (such as when being used over a pipe) Tcl uses full buffering for its standard output. You want line buffering (or perhaps none for unbuffered output) so that you can see values immediately when they're written out.

You will probably also want to run a custom REPL (assuming Tcl 8.5 or 8.6):

fconfigure stdout -buffering line
while {[gets stdin line] >= 0} {
    set code [catch $line msg opt]
    puts [list $code $msg $opt]
}

The way this writes the result back is as a Tcl list (not usually too hard to parse) which contains the result code (usually 0 for OK or 1 for ERROR) the result message/data, and a key/value dictionary that describes additional “interesting” stuff like stack traces in the case of errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM