0

我的应用程序运行良好,解决了我寻求帮助的所有问题,最好是 :) 我想做的最后一件事是能够打印(或制作报告)从数据库查询的各种字符串。我搜索了很多,找到了 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
4

2 回答 2

1

这段代码在很多地方都是错误的......
我修改了你的代码,进行了许多改进和适当的行长:

def preparePrint(self):
    caseNumber = str(self.ui.linecaseNumber.text()).upper()
    if caseNumber:
        with con:
            cur = con.cursor()
            # fetch 3 values from SQLite database
            cur.execute('SELECT notes FROM cases WHERE caseNumber = ?',
                        caseNumber)
            tempnotes = cur.fetchone()[0]
            # if you have multiple columns, use:
            # tempnotes = ''.join(cur.fetchone())
            cur.execute('SELECT name FROM cases WHERE caseNumber = ?',
                        caseNumber)
            tempname = cur.fetchone()[0]
            cur.execute('SELECT date FROM cases WHERE caseNumber = ?',
                        caseNumber)
            tempdate = cur.fetchone()[0]

        # no need to 'get rid of "tuple marks"',
        # because they aren't there now

        # join them in one string. Notice the \n
        finalText = ("NAME: %s\nDATE: %s\nNOTES: %s"
                     % (tempname, tempdate, tempnotes))

        # put the joined string into one QTextEdit, 
        # while preserving HTML properties (bold mostly).

        # again, no need to 'get rid of "tuple marks"'
        self.ui.textPrint.setHtml(finaltext)

这应该使文本带有换行符。如果没有,请尝试使用\r\nin finaltext 而不是\n. 如果它仍然不起作用,那么就会发生一些奇怪的事情,但至少你有更好的代码。

于 2012-07-14T12:20:24.437 回答
0

我破解了!:D 由于 QTextEdit 接受 HTML,<br> 标签会换行。这是一段与原始帖子不同的代码:

finalText = (str("NAME: %s%sDATE: %s%sNOTES: %s%s" %(tempname,'<br>',tempdate,'<br>',tempnotes,'<br>')))
self.ui.textPrint.setHtml(str(finalText).replace('\\n','\r'))
于 2012-07-14T12:36:24.010 回答