How to send bunch of strings to printer?

  • Hello everyone,

    my application is coming up nicely, with all the problems I've asked help for it better be :) Last thing I wanted to make is the ability to print (or make a report) various strings queried from database. I googled a lot, found ReportLab Toolkit, reStructuredText and more, but that is not what I am looking for.

    Here is my code:

    @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])
    cur.execute('SELECT name FROM cases WHERE caseNumber =?',[caseNumber])
    cur.execute('SELECT date FROM cases WHERE caseNumber =?',[caseNumber])
    tempdate = str(cur.fetchone()) #fetch 3 values from SQLite database

    tempname = (str(tempname.replace('(','').replace(')','').replace(',','').replace("'","")))
    tempnotes = (str(tempnotes.replace('(','').replace(')','').replace(',','').replace("'","")))
    tempdate=(str(tempdate.replace('(','').replace(')','').replace(',','').replace("'",""))) #get rid of "tuple marks"

    finalText = (str("NAME: %s\nDATE: %s\nNOTES: %s" %(tempname,tempdate,tempnotes))) #join them in one string. Notice the \n
    self.ui.textPrint.setHtml(str(finalText.replace('(','').replace(')','').replace(',','').replace("'","").replace('\n','\r'))) #put the joined string into one QTextEdit, while preserving HTML properties (bold mostly).@

    After this function, there is one for creating PDF which works EXCEPT newline characters. When I print finalText to console it looks the way I want it to. It does not look like that in QTextEdit :(

    Console output:
    Name: Some Name
    Date: 14.07.2012
    Notes: Some notes go here@

    QTextEdit output:

    Name: Some Name Date: 14.07.2012 Notes: Some notes go here@

    It IGNORES replacement of \n with \r . Same replace works nicely in another QTextEdit. Am I doing something wrong?

  • I cracked it! :D

    Since QTextEdit accepts HTML, <br> tag will make a newline. Here is a piece of code that is different from original post.

    @finalText = (str("NAME: %s%sDATE: %s%sNOTES: %s%s" %(tempname,'<br>',tempdate,'<br>',tempnotes,'<br>')))


    Might not be convenient for larger pieces of text, but one A4 page (which is my aim) won't be a problem.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.