⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

How to print an image and scale it to fit page?

  • Hello,
    I use the following code to successfully print an image, but it fills the entire 8.5x11 piece of paper and is chopped off on the sides. What must I add so that it scales to fit the page?

    QImage img = myPixmap.toImage(); // myPixmap previously exists
    QPrinter printer;
    QPrintDialog *dlg = new QPrintDialog(&printer,0);
    if(dlg->exec() == QDialog::Accepted) {
        QPainter painter(&printer);

  • Lifetime Qt Champion


    It's been some time but isn't it something you set in the QPrintDialog ?

  • That was my first thought too, but apparently not. (unless that's due to my own printer..)

  • @pmh4514 said:


    You can get paper size from printer and draw image by this function
    void QPainter::drawImage ( const QRectF & target, const QImage & image, const QRectF & source, Qt::ImageConversionFlags flags = Qt::AutoColor )

  • it doesn't seem to matter what rectangle values I pass to drawImage, or if it's based on the paper size or even if I try a dummy rect that is like (0,0,50,50) - the resulting print still occupies the entire 8.5x11 sheet of paper and is cutoff on the right..