[Qt-interest] [SOLVED] How to print out widgets?

M. Bashir Al-Noimi admin at mbnoimi.net
Thu Jun 17 02:34:09 CEST 2010


Hi again,

Thanks a lot now this fix the issue now you can take a look for complete 
snippet:
QPrinter printer;

     printer.setPageSize(QPrinter::A4);

     printer.setResolution(600);

     QPrintDialog  print(&printer  ,  this);

     QPixmap  pixmap(ui->tableWidget->size());

     QPainter  painter;

     if(print.exec()==  QPrintDialog::Accepted)

     {

         ui->tableWidget->render(&pixmap);

         painter.begin(&printer);

         printer.paintEngine()->drawPixmap(QRectF(0,  0,  pixmap.width(),  pixmap.height()),  pixmap,  QRectF(0,  0,  pixmap.width(),  pixmap.height()));

         painter.end();

     }



On 16/06/2010 12:59 ص, Jason H wrote:
> Correcting/Adding to your code:
> QPixmap pixmap(ui.tableWidget->height(), ui.tableWidget->width());
> QPrinter printer;
> ...
> printer->paintEngine()->drawPixmap(QRectf(0,0, pixmap.width(), 
> pixmap.height(), pixmap)
>
> Should work.
>
> ------------------------------------------------------------------------
> *From:* M. Bashir Al-Noimi <admin at mbnoimi.net>
> *To:* qt-interest at trolltech.com
> *Sent:* Tue, June 15, 2010 7:34:00 PM
> *Subject:* Re: [Qt-interest] How to print out widgets?
>
> Oh please I just want a snippet. You gave me is a theoretically 
> solution I and couldn't find any functions can do what you said. I 
> spent more than 8 hours (I lost my mind now :'( ) and I didn't find 
> any solution... just correct my snippet or wrote new one please (don't 
> take about chunk now I want to make it works first):
>
> QPrinter printer;
>      printer.setPageSize(QPrinter::A4);
>      printer.setResolution(600);
>      QPrintDialog  print(&printer  ,  this);
>      QPixmap  pixmap(printer.paperSize(QPrinter::DevicePixel).toSize());
>      QPainter  painter(&pixmap);
>      if(print.exec()==  QPrintDialog::Accepted)
>      {
>          ui->tableWidget->setFixedWidth(250);
>          ui->tableWidget->render(&pixmap,  QPoint(100,  100));
>          //I  know  this  wrong  line  but  I  can't  find
>   a  way  for  printing  pixmap
>          painter.begin(&printer);
>      }
>
>
> On 15/06/2010 11:58 م, Jason H wrote:
>> Oh dear, don't do that! You'll get a HUGE chunk of memory that you 
>> don't need.
>>
>> 1. use render() to the pixmap, like you have.
>> 2. then use QPrinter/QPrintDevice to draw the pixmap on the printer 
>> at some coordinate (0,0?).  I believe you call this 'painter'
>>
>> Between 1 and 2, you might want to scale the pixmap to account for 
>> DPI differences. (scaled(300/72.0))
>>
>> ------------------------------------------------------------------------
>> *From:* M. Bashir Al-Noimi <admin at mbnoimi.net>
>> *To:* Jason H <scorp1us at yahoo.com>
>> *Cc:* qt-interest at trolltech.com
>> *Sent:* Tue, June 15, 2010 6:25:16 PM
>> *Subject:* Re: [Qt-interest] How to print out widgets?
>>
>> On 15/06/2010 06:02 م, Jason H wrote:
>>> I would probably try not rendering directly to a printer, bu rather 
>>> a pixmap, that is then scaled to the printer. It is likely that the 
>>> drawing commands to the printer did not translate properly. Printers 
>>> deal with bitmaps, text, or vectors, and there are numerous quirks 
>>> to them. By capturing it as a pixmap first, you'll then load an 
>>> image to the printer, rather than some mixed-mode rendering. I'd 
>>> expect render() to try a combination of vectors and bitmaps. Also, 
>>> coordinate spaces are different.
>>>
>>> So capture as an image, then print the image.
>> I got you idea but I don't know how to print the image itself usual 
>> widgets has render or print functions where images don't, so could 
>> you please give a tiny snippet?
>>
>> PS.
>> I looked into this issue in the archive and discoverd that all 
>> related discussions threads not completed.
>> I tried to use this but it's not working because painter does nothing 
>> (I need some corrections):
>> QPrinter printer;
>>      printer.setPageSize(QPrinter::A4);
>>      printer.setResolution(600);
>>      QPrintDialog  print(&printer  ,  this);
>>      QPixmap  pixmap(printer.paperSize(QPrinter::DevicePixel).toSize());
>>      QPainter  painter(&printer);
>>      if(print.exec()==  QPrintDialog::Accepted)
>>      {
>>          ui->statusBar->showMessage("printing  in  progress",  1000);
>>          ui->tableWidget->setFixedWidth(250);
>>          ui->tableWidget->render(&pixmap,  QPoint(100,  100));
>>      }
>>
>>
>>>
>>>
>>>
>>>
>>> ------------------------------------------------------------------------
>>> *From:* M. Bashir Al-Noimi <admin at mbnoimi.net>
>>> *To:* qt-interest at trolltech.com
>>> *Sent:* Tue, June 15, 2010 12:15:25 AM
>>> *Subject:* [Qt-interest] How to print out widgets?
>>>
>>> Hi folks,
>>>
>>>
>>> I tried to print out QTableWidget as shown below:
>>>
>>>
>>> QPrinter printer;
>>>
>>>      printer.setPageSize(QPrinter::A4);
>>>      QPrintDialog  print(&printer  ,  this);
>>>      QPainter  painter(&printer);
>>>      if(print.exec()==  QPrintDialog::Accepted)
>>>      {
>>>          ui->tableWidget->setFixedWidth(250);
>>>          ui->tableWidget->render(&painter,  QPoint(100,  100));
>>>      }
>>>
>>>
>>> but it didn't print out anything and gave me the following error 
>>> message instead:
>>>
>>>
>>> QWin32PrintEngine::drawPixmap, StretchBlt failed (Not enough storage 
>>> is available to process this command.)
>>>
>>> How I can fix this issue?
>>>
>>>
>>>
>>

-- 
Best Regards
Muhammad Bashir Al-Noimi
My Blog: http://mbnoimi.net

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100617/95da1d57/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: admin.vcf
Type: text/x-vcard
Size: 414 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100617/95da1d57/attachment.vcf 


More information about the Qt-interest-old mailing list