Important: Please read the Qt Code of Conduct -

Printer - One Record per page.

  • Hi friends,

    Having a sqlite datafile (id,name,agreement) I wanted send to printer
    the sorted names of all records, with yes at agreement, but each name at center
    of 1 page, using only plain python and Qt. But Im lost to
    figure how print each sqlite name field 1 per page in Qt.

    Some gentle soul can post a simple snippet code of the process?
    Anna->Eject A4 page->Cindy->Eject A4 page->Mary->Eject A4 page

    Thanks in advance

  • What exactly do you have trouble with? Is it how to print in general or only printing one name a page?

  • Hi,

    The focus is: select 1 name and print it at center of a4 page -> do it with all names of sqlite database. blushes

    What im getting is many pages with all names of database filling the page area.

  • Here you go (untested but it should work):
    QPrinter printer;
    QPainter painter;
    QSqlQuery query("SELECT * FROM table");
    while ( {
    QString name = query.value(0).toString();
    QRect textSize = painter.front().boundingRect(name);

  • Hum... i converted the snippet to python but running it nothing happens...
    (im away from printer now so choose to test it printing to pdf)

    @from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from PyQt4.QtSql import *

    db = QSqlDatabase.addDatabase("QSQLITE")

    printer = QPrinter(QPrinter.HighResolution)

    painter = QPainter(printer)

    query = QSqlQuery("SELECT * FROM insc")

    start = painter.begin()

    name = QString(query.value(0).toString())
    textSize = QRect(painter.front().boundingRect(name))
    printer.drawText = QRectF((printer.width()-textSize.width)/2,(printer.height()-textSize.height())/2,textSize.width(),textSize.height())

  • shouldn't it be
    @ (I'm not that familiar with python, but it's still a method, right?

  • Oops. Worked. Thank you very much.

  • Great! Could you please add [Solved] in front of the title? (Edit the first post)

Log in to reply