2012-07-14 69 views
0

我的應用程序很好,我已經問了所有問題,幫助它更好:)我最想做的是能夠打印(或製作報告)各種從數據庫查詢字符串。我搜索了很多,發現了ReportLab Toolkit,reStructuredText等,但這不是我正在尋找的。PyQt:發送多個字符串到打印機

這裏是我的代碼:(縮進可能會稍微偏離,因爲粘貼)

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其中工程EXCEPT換行符。當我打印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 
+0

嘗試'\ r \的N'而不只是'\ N' – BrtH 2012-07-14 11:03:06

+0

在_finalText_?它也是一樣的,沒有換行符。 – ivica 2012-07-14 11:16:56

+0

爲什麼在最後使用'.replace('\\ n','\ r')'?刪除它,然後重試。 – BrtH 2012-07-14 12:05:08

回答

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) 

這應該使文本換行。如果不是,則在finaltext中嘗試使用\r\n而不是\n。如果它仍然不起作用,那麼奇怪的是,但至少你有更好的代碼。

+0

謝謝你的回答。我的代碼很可能不好,我是自學成才的初學者。但是,您的代碼無法正常工作。 SQLite抱怨查詢,錯誤「sqlite3.ProgrammingError:提供的綁定數量不正確。當前語句使用1,並且提供了3個。」 查詢工作(可能是錯誤的,但工程)是: 'cur.execute( 'SELECT筆記情況下caseNumber =?', [caseNumber])' 我的意思並不是要尊重我非常感謝您的意見和答案。 – ivica 2012-07-14 12:57:58

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'))