browser = QTextBrowser() #实例化一个textbrowser
browser.append(‘sdfsdfds’) #追加内容
browser.setOpenLinks(True) #打开文档内部链接 默认为True
browser.setOpenExternalLinks(True) #打开外部链接 默认false 当openlinks设置false时 该选项无效
textbrowser.setSearchPaths([“ldks”,":/sdfs"]) #设置文档搜索路径 参数为包含目录的List
textbrowser.setSource(“index.html”) #设置文档
dt=textbrowser.documentTitle() #返回文档的标题
self.connect(textbrowser,SIGNAL(“SourceChanged(QUrl)”),self.update) #发出一个SourceChanged(QUrl)信号
textbrowser同时 具有以下插槽: home() :返回主文档, backward() #返回上一文档,forward()前进
browser.setDocumentTitle(‘dsds’) #设置文档标题
例子:
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *class Form(QDialog):def __init__(self, parent=None):super(Form, self).__init__(parent)self.browser = QTextBrowser()self.lineedit = QLineEdit("Type an expression and press Enter")#self.lineedit.selectAll()layout = QVBoxLayout() #垂直盒式布局layout.addWidget(self.browser)layout.addWidget(self.lineedit)#layout = QGridLayout() #网格布局#layout.addWidget(self.browser,0, 0) #layout.addWidget(self.lineedit,0, 0)self.setLayout(layout)#self.lineedit.setFocus()self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi) #信号绑定到槽,按回车后执行槽函数self.setWindowTitle("Calculate")def updateUi(self):try:text = unicode(self.lineedit.text())self.browser.append("%s = <b>%s</b>" % (text, eval(text))) #显示内容支撑html格式语法,eval返回表达式结果except:self.browser.append("<font color=red>%s is invalid!</font>" % text)app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
程序效果如下:
该程序通过QTextBrowser控件可实现在下方输入一个表达式,然后在上方显示出来。