how to show photo in QPrintPreviewDialog



  • hi
    i want to print a photo & before printing show a preview

    i print a photo whit this code:

    QPrinter PhotoPrint;
    
        QPrintDialog *PhotoPrintDialog = new QPrintDialog(&PhotoPrint,0);
    
        if(PhotoPrintDialog->exec() == QDialog::Accepted)
        {
            QImage Photo(ui->PhotoLable->pixmap()->toImage());
            QPainter painter(&PhotoPrint);
            painter.drawImage(QPoint(0,0),Photo);
            painter.end();
        }
    

    how to show a preview before print?


  • Moderators

    Hi! You can use QPrintPreviewDialog Class for this.



  • @Wieland Hi
    i know this, can show me example for photo


  • Moderators

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPixmap>
    #include <QPrinter>
    #include <QPrintDialog>
    #include <QPrintPreviewDialog>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void printerPaint(QPrinter *printer);
    
    private slots:
        void on_pushButton_clicked();
        void on_pushButton_2_clicked();
    
    private:
        Ui::MainWindow *ui;
        QPixmap m_pixmap{"/home/patrick/m.png"};
        QPrinter m_printer;
        QPrintDialog m_printDialog{&m_printer};
        QPrintPreviewDialog m_printPreviewDialog{&m_printer};
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QPainter>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->label->setPixmap(m_pixmap);
        connect(&m_printPreviewDialog, &QPrintPreviewDialog::paintRequested,
                this, &MainWindow::printerPaint);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::printerPaint(QPrinter *printer)
    {
        QPainter painter(printer);
        painter.drawPixmap(QPoint(0,0), m_pixmap);
    }
    
    // Print without preview
    void MainWindow::on_pushButton_clicked()
    {
        if (m_printDialog.exec() == QDialog::Accepted)
            printerPaint(&m_printer);
    }
    
    // Show preview, potentially print from this dialog
    void MainWindow::on_pushButton_2_clicked()
    {
        m_printPreviewDialog.exec();
    }
    


  • @Wieland thank but whare you tell qprintperview set a pohto for preview?


  • Moderators

    When the preview dialog is executed it emits a signal and that is connected to MainWindow::printerPaint. Inside the latter function we paint the photo. The photo is stored inside m_pixmap which is initialized in the header file in this example. Printing without preview uses MainWindow::printerPaint, too, so we don't have to copy and paste the exact same code again.


Log in to reply
 

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