简体   繁体   English

Qt creator 4.11,在应用程序 output 面板中创建链接

[英]Qt creator 4.11, create a link in the application output panel

I desesperatly try to find a way to make the application output panel a bit more useful by printing an error with a file path and a line number (basically _ FILE _ and _ LINE _ macros) and make it clickable from the pannel to go directly in the source file in the IDE.我绝望地尝试找到一种方法,通过打印带有文件路径和行号的错误(基本上是_FILE_和_LINE_宏)并使其可从面板直接单击到go ,从而使应用程序output面板更有用在 IDE 的源文件中。

Is it possible to do so with std::cout only?是否可以仅使用 std::cout 这样做?

I found a post on stack which doesn't work with my need.我发现堆栈上的帖子不符合我的需要。

The mechanic you need to use here is ANSI escape sequences.您需要在这里使用的机制是 ANSI 转义序列。

ANSI escape sequences are processed by (mostly) Unix terminals and terminal emulators for changing the terminal behavior, eg formatting or coloring text. ANSI 转义序列由(主要)Unix 终端和终端仿真器处理,用于更改终端行为,例如格式化或着色文本。 More recently, hyperlinks may be embedded using an escape sequence as well.最近,也可以使用转义序列嵌入超链接。 For example, the ls utility may embed file:// scheme links with the printed filenames and a terminal may allow to open a file by clicking on it.例如, ls实用程序可以嵌入带有打印文件名的file://方案链接,并且终端可以允许通过单击文件来打开文件。 And GCC does this as well (see -fdiagnostics-urls option). GCC 也可以做到这一点(参见-fdiagnostics-urls选项)。

Several IDEs nowadays also support these links in their output panes.现在的几个 IDE 在其 output 窗格中也支持这些链接。 To form a link you need to print one escape sequence before the text and one after (to reset the link state), like so:要形成链接,您需要在文本之前和之后打印一个转义序列(以重置链接状态),如下所示:

printf '\e]8;;http://example.com\e\\This is a link\e]8;;\e\\\n'

Note that \e is ESC, the other characters in the example are regular characters as printed.注意\e是 ESC,示例中的其他字符是打印的常规字符。 Find a good documentation about this, esp.找到一个很好的文档,特别是。 about how to form appropriate file:// URIs here .关于如何在此处形成适当的file:// URI。

For Qt Creator to recognize a link to a source file in the Application Output, it needs to be in a specific format.为了让 Qt Creator 识别应用程序 Output 中源文件的链接,它需要采用特定格式。 In my tests I've found the following to work:在我的测试中,我发现以下工作:

file:///home/user/project/src/foo.cpp:1234

This follows the pattern file://%{file}:%{line} .这遵循模式file://%{file}:%{line}

Since this question is related to Qt, you may want to set the QT_MESSAGE_PATTERN environment variable such that the file and line number is automatically included in your debug messages.由于此问题与 Qt 有关,您可能需要设置QT_MESSAGE_PATTERN环境变量,以便文件和行号自动包含在调试消息中。 For example:例如:

QT_MESSAGE_PATTERN="%{message} (file://%{file}:%{line})"

For more information, see:有关更多信息,请参阅:

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

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