简体   繁体   English

将Qt GUI与TCL应用程序连接

[英]Connecting Qt GUI with TCL application

I have a Qt GUI and another application that communicates with the user using TCL interpreter. 我有一个Qt GUI和另一个使用TCL解释器与用户通信的应用程序。

I want the Qt and the application to share information created in the application and be shown in GUI. 我希望Qt和应用程序共享在应用程序中创建的信息并在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. 另外,我希望Qt生成字符串,该字符串将通过控制台发送到另一个线程中的TCL(应用程序)。

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! 我的想法是将TCL解释器stdin更改为Qt控制台的stdout ,但是我不知道该怎么做!

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 ). 在Tcl应用程序的localhost接口上创建一个网络套接字,并侦听来自Qt应用程序的命令(请参阅socketfileeventinfo 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. 我建议您处理从属解释器( interp create )中收到的命令,以便可以轻松地沙沙远程应用程序允许的命令。 To pass strings to the Tcl application your Qt app can just send to the socket. 要将字符串传递到Tcl应用程序,您的Qt应用程序可以只发送到套接字。 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. 如果确保仅在localhost接口上打开侦听套接字,则可以消除大多数安全问题。 You could look into adding tls if you run into problems or arrange to read a named pipe instead. 如果遇到问题或安排读取命名管道,则可以考虑添加tls。 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. 如果要捕获Tcl解释器的标准输出(以及标准错误;一些重要消息!),则必须在子进程中运行Tcl解释器,并通过管道与基于Qt / C ++的外部进程进行通信。 (You should be able to find how to do that in general by searching Stack Overflow…) (您应该可以通过搜索Stack Overflow来找到一般的操作方法...)

Tcl-specific details: You'll really want to make sure that the Tcl interpreter does: 特定于Tcl的详细信息:您真的要确保Tcl解释器可以:

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. 这是因为在非交互模式下(例如在管道上使用时),Tcl将完全缓冲用于其标准输出。 You want line buffering (or perhaps none for unbuffered output) so that you can see values immediately when they're written out. 你要行缓冲(或者none用于缓冲输出),这样就可以立即看到价值时,他们写出来。

You will probably also want to run a custom REPL (assuming Tcl 8.5 or 8.6): 您可能还希望运行自定义的REPL(假设Tcl 8.5或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. 将结果写回的方式是作为Tcl列表(通常不太难解析),其中包含结果代码(通常为0表示OK或1表示ERROR),结果消息/数据以及描述其他内容的键/值字典在发生错误的情况下,诸如堆栈跟踪之类的“有趣”东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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