问题描述
我有一个QTextBrowser,我输入了该示例文本:
I have a QTextBrowser where I've entered that sample text:
Column1 Column2
1 2 3 4 www.google.com
列之间有标签,数字之间有空格。 plainText = QTextBrowser.toPlainText()给出:
Between the columns there are tabs and between the numbers there are spaces. plainText=QTextBrowser.toPlainText() gives:
Column1 Column2
1 2 3 4 www.google.com
现在,我想让链接可点击。通过这个,我将文本设置为html。
我写了一些字符串操作
Now, i want to make the link clickable. By this, I'll set the text as html.I wrote some string operations with
WORDS = re.split("(\W+)",plainText)
这给出了
['Column1', '\t', 'Column2', '\n', '1', ' ', '2', ' ', '3', ' ', '4', '\t', 'www', '.', 'google', '.', 'com', ' ', '']
并用html格式的超链接替换www
and replaced "www" with the hyperlink in html-format
<a href=http://www.google.com>www.google.com</a>
然后,我删除了WORDS中的项目:。,test,。, com。
Then, I deleted the items in WORDS: ".", "test", ".", "com".
['Column1', '\t', 'Column2', '\n', '1', ' ', '2', ' ', '3', ' ', '4', '\t', '<a href=http://www.google.com>www.google.com</a>', ' ', '']
然后我重建所有的WORDS部分再次使用字符串,将换行符\ n替换为
Then I rebuild all the WORDS-parts again to a string, replacing the linebreak \n with
<br/>.
现在,当我将字符串设置为QTextBrowser时,链接显示正确且可点击。问题是:输入的文本,因为它现在是html,忽略/删除所有空格(如果不止一个)和标签!
输出看起来像(链接现在是蓝色并加下划线)
Now, when I set the string to the QTextBrowser, the link is displayed correctly and also clickable. Problem is: The entered text, as it is now html, ignores/removes all spaces (if more than one) and tabs!The output looks like (with the link now being blue and underlined)
Column1 Column2
1 2 3 4 www.google.com
如何在不破坏格式的情况下制作超链接?
How can I just make a hyperlink without the formatting being destroyed?
也许我在错误的火车上?
Perhaps I am on the wrong train??
推荐答案
我想我明白了。如果它可能有用,这是我的解决方案:
一个html文件确实尊重你的格式,如果你把css中的样式设置为pre,而不是像QTextBrowser那样预先包装。
此外,最后可以直接用创建的超链接替换键入的链接。
I think I got it. In case it might be useful, here is my solution:A html file does respect your format, if you put the style in the css to "pre", and not like QTextBrowser to "pre-wrap".Moreover, in the end the typed links can directly be replaced by the created hyperlinks.
生成并设置到QTextBrowser的html文件然后partwise看起来喜欢:
The html file that is generated and setted to the QTextBrowser then partwise looks like:
<style type="text/css">#editor { white-space: pre; }</style>
<p id='editor', style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><a name="editor"></a>Column1 Column2</p>
这篇关于PyQt4中的QTextBrowser超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!