June 9, 2011

AnnaBach AnnaBach
Lab Rat
3 posts

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

7 replies

June 10, 2011

loladiro loladiro
Lab Rat
594 posts

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

June 10, 2011

AnnaBach AnnaBach
Lab Rat
3 posts

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.

June 10, 2011

loladiro loladiro
Lab Rat
594 posts

Here you go (untested but it should work):

  1.    
  2.       QPrinter printer;
  3.       QPainter painter;
  4.       QSqlQuery query("SELECT * FROM table");
  5.       painter.begin(&printer);
  6.       while (query.next()) {
  7.          QString name = query.value(0).toString();
  8.          QRect textSize = painter.front().boundingRect(name);
  9.          printer.drawText(QRectF((printer.width()-textSize.width)/2,(printer.height()-textSize.height())/2,textSize.width(),textSize.height()),Qt::AlignCenter,name)
  10.          printer.newPage();
  11.      }
  12.      painter.end();

June 10, 2011

AnnaBach AnnaBach
Lab Rat
3 posts

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)

  1. from PyQt4.QtCore import *
  2. from PyQt4.QtGui import *
  3. from PyQt4.QtSql import *
  4.  
  5. db = QSqlDatabase.addDatabase("QSQLITE")
  6. db.setDatabaseName("data.db")
  7.  
  8. printer = QPrinter(QPrinter.HighResolution)
  9. printer.setPageSize(QPrinter.A4)
  10. printer.setOutputFormat(QPrinter.PdfFormat)
  11. printer.setOutputFileName("test.pdf")
  12.  
  13. painter = QPainter(printer)
  14.  
  15. query = QSqlQuery("SELECT * FROM insc")
  16.  
  17. start = painter.begin()
  18.  
  19. while query.next():
  20.     name = QString(query.value(0).toString())
  21.     textSize = QRect(painter.front().boundingRect(name))
  22.     printer.drawText = QRectF((printer.width()-textSize.width)/2,(printer.height()-textSize.height())/2,textSize.width(),textSize.height())
  23.     printer.newPage()
  24. painter.end()

June 10, 2011

loladiro loladiro
Lab Rat
594 posts

shouldn’t it be

  1. printer.drawText(QRectF((printer.width()-textSize.width)/2,(printer.height()-textSize.height())/2,textSize.width(),textSize.height()))
(I’m not that familiar with python, but it’s still a method, right?

June 14, 2011

AnnaBach AnnaBach
Lab Rat
3 posts

Oops. Worked. Thank you very much.

June 14, 2011

loladiro loladiro
Lab Rat
594 posts

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

 
  ‹‹ [Moved] How to write a cache class from QAbstractNetworkCache with python      To Subclass NSApplication in QApplication. ››

You must log in to post a reply. Not a member yet? Register here!