主要有几个问题:
1、后台单独线程处理数据的问题;最初不明白QT在子线程中不能操作GUI的问题,因此查了很久的GUI的crash的问题
2、界面的布局;理解了gridLayout这个非常方便的布局方法。
代码片段
# -*- coding: utf-8 -*-
"""
Module implementing umd_MainWindow.
"""
import sys, os, re, time,sip
from PyQt4 import QtGui, QtCore
from dict4ini import DictIni
from Ui_ebook_txt import Ui_umd_MainWindow
class eBookException(Exception):
pass
class ebookParseWorker(QtCore.QThread):
'''QT中子线程内不能操作GUI界面,切记切记'''
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
self.exiting = False
self.isWait=False
self.data={}
def setVar(self, name, value):
self.data[name]=value
def __del__(self):
self.exiting = True
self.wait()
def buildRegx(self):
#self.alert(regx)
if self.data['chmChapterRegx'] is None:
regx='pages\[(\d+)\]\s*=\s*\[(.*)\]'
try:
self.chapterInfo=re.compile(self.data['chmChapterRegx'])
except:
self.alert('列表处理正则表达式不正确')
return False
self.chapterList=[]
return True
def getChapterList(self):
if os.path.exists(unicode(self.data['chmChapterJs'])) ==False:
self.alert('章节列表JS文件%s不存在'%(self.data['chmChapterJs']))
return False
if self.data['inputCharset'] is None or self.data['inputCharset'].strip()=='':
self.data['inputCharset']='gbk'
m=self.chapterInfo.findall(open(unicode(self.data['chmChapterJs']),'rb').read())
vol=''
for x in m:
oldChapterInfo=x[1].split(',')
newChapterInfo={}
if len(oldChapterInfo)==4:
if oldChapterInfo[3].strip('"').strip("'")[0:5]!='<img alt="" /><1:
self.alert('章节列表小于1,请检查列表正则表达式是否正确')
return False
return self.chapterList
def run(self):
#是否触发错误
error=False
self.alert("<span><strong>开始处理章节内容</strong>")
if self.data['inputCharset'] is None or self.data['inputCharset'].strip()=='':
self.data['inputCharset']='gbk'
if self.data['outputCharset'] is None or self.data['outputCharset'].strip()=='':
self.data['outputCharset']='gbk'
if self.data['oldChapterTxtDir'] is None or self.data['oldChapterTxtDir'].strip()=='' :
self.alert('原始文件存放位置为空')
error=True
if self.data['newChapterTxtDir'] is None or self.data['newChapterTxtDir'].strip()=='':
self.data['newChapterTxtDir']=self.data['oldChapterTxtDir']
if os.path.exists(unicode(self.data['newChapterTxtDir'])) ==False:
使用Webkit library (可以说是纯QT实现)代码量不多,直接贴代码 (读起来一点不痛苦的) :
myWebView = new QWebView(this)//this 是main window widget, myWebView 是它的成员变量
myWebView->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true)
myWebView->page()->settings()->setAttribute(QWebSettings::PluginsEnabled,true)
myWebView->page()->mainFrame()->addToJavaScriptWindowObject("mainWindowObject", this)//html页面中,可以通过"mainWindowObject"这个对象名访问主控件中的方法 (slot)
setCentralWidget(myWebView)
myWebView->setUrl( xxx )//xxx是你的url或本地html路径
//. . .
class MainWindow : public QMainWindow
{
//. . .
public slots:
void CPlusPlusFunction(const QString&str) //这个函数是将被JavaScript调用的
{
myWebView->page()->mainFrame()->uateJavaScript( QObject::tr("jsFunction('Popup Dialog')") )
}
}
HTML文件内容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>myjstest</title>
<script language="JavaScript" type="text/javascript">
function jsFunction(values) //this function will be called from C++ codes
{
alert(values)
}
function test()
{
mainWindowObject.CPlusPlusFunction( "calling C++ function from javaScript" )
}
</script>
</head>
<body>
<div id="dest"></div><form action="" method="post">
<input type="button" name="" value="myTest" onclick="test()" />
</form>
</body>
</html>
这种方法,使用Webkit作为浏览器,如果你的页面使用了ActiveX控件(比如google earth插件),则不能正常工作。
这种情况下,你需要放弃Webkit,在主程序中调用IE 控件(WebBrowser Control)作为浏览器。(但是这样也失去了跨平台的支持,因为IE只能在Wndosw上跑。)