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 = %s" % (text, eval(text))) #显示内容支撑html格式语法,eval返回表达式结果
except:
self.browser.append(
"%s is invalid!" % text)
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()