我的应用程序运行良好,解决了我寻求帮助的所有问题,最好是 :) 我想做的最后一件事是能够打印(或制作报告)从数据库查询的各种字符串。我搜索了很多,找到了 ReportLab Toolkit、reStructuredText 等等,但这不是我想要的。
这是我的代码:(由于粘贴,缩进可能会稍微偏离)
def preparePrint(self):
caseNumber = (str(self.ui.linecaseNumber.text()).upper())
if caseNumber:
with con:
cur = con.cursor()
cur.execute('SELECT notes FROM cases WHERE caseNumber =?',[caseNumber])
tempnotes=str(cur.fetchone())
cur.execute('SELECT name FROM cases WHERE caseNumber =?',[caseNumber])
tempname=str(cur.fetchone())
cur.execute('SELECT date FROM cases WHERE caseNumber =?',[caseNumber])
tempdate = str(cur.fetchone()) #fetch 3 values from SQLite database
#get rid of "tuple marks"
tempname = (str(tempname.replace('(','').replace(')','').replace(',','').replace("'","")))
tempnotes = (str(tempnotes.replace('(','').replace(')','').replace(',','').replace("'","")))
tempdate=(str(tempdate.replace('(','').replace(')','').replace(',','').replace("'","")))
#join them in one string. Notice the \n
finalText = (str("NAME: %s\nDATE: %s\nNOTES: %s" %(tempname,tempdate,tempnotes)))
#put the joined string into one QTextEdit, while preserving HTML properties (bold mostly).
self.ui.textPrint.setHtml(str(finalText.replace('(','').replace(')','').replace(',','').replace("'","").replace('\\n','\r')))
在此功能之后,还有一个用于创建 PDF 的功能,除了换行符。当我将 finalText打印到控制台时,它看起来就像我想要的那样。它在 QTextEdit 中看起来不像 :(
控制台输出:
Name: Some Name
Date: 14.07.2012
Notes: Some notes go here
QTextEdit 和 PDF 输出:
Name: Some Name Date: 14.07.2012 Notes: Some notes go here