2013-07-14 41 views
0

我有一个QTextBrowser在那里我已经输入的示例文本:QTextBrowser超链接PyQt4中

Column1 Column2 
1 2 3 4 www.google.com 

有标签列之间和有空格的数字之间。 plainText = QTextBrowser.toPlainText()给出:

Column1 Column2 
1 2 3 4 www.google.com 

现在,我想使链接可点击。通过这个,我将文本设置为html。 我写了一些字符串操作与

WORDS = re.split("(\W+)",plainText) 

这让

['Column1', '\t', 'Column2', '\n', '1', ' ', '2', ' ', '3', ' ', '4', '\t', 'www', '.', 'google', '.', 'com', ' ', ''] 

,取而代之的 “www” 在HTML格式的超级链接

<a href=http://www.google.com>www.google.com</a> 

然后,我删除了词的项目:“。”,“test”,“。”,“com”。

['Column1', '\t', 'Column2', '\n', '1', ' ', '2', ' ', '3', ' ', '4', '\t', '<a href=http://www.google.com>www.google.com</a>', ' ', ''] 

然后我再重建所有的话,零件的字符串,

<br/>. 

现在更换断行\ N,当我设置字符串到QTextBrowser,链接正确显示和也可点击。问题是:输入的文本,因为它是现在的HTML,忽略/删除所有空格(如果不止一个)和制表符! 输出类似于(该链接现在是蓝色并带有下划线)

Column1 Column2 
1 2 3 4 www.google.com 

我如何才能让没有格式化的超链接被破坏?

也许我在错列车上?

+0

我想我已经得到了它。 – user2366975

回答

0

我想我明白了。如果它可能是有用的,这里是我的解决方案: 一个html文件确实尊重你的格式,如果你把CSS中的样式设置为“pre”,而不是像QTextBrowser那样“预包装”。 此外,键入的链接最终可以直接被创建的超链接替换。

已生成并设置好的给QTextBrowser的HTML文件,然后partwise样子:

<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>