当前位置:网站首页>Text editor for QT project practice - Episode 12

Text editor for QT project practice - Episode 12

2022-06-25 01:00:00 PureヾChan

The previous implementation of the document text and paragraph operation , Next, we are going to print documents and preview them .

Document printing implementation :

towards mainWindow.h Add function :

public:
 void docPrint();// Document printing 
private slots:
 void on_printAction_triggered();

Slot function on_PrintAction_triggered() A function is to call docPrint() function .

Function implementation :

void MainWindow::docPrint()
{
    // Printing, of course, requires a printer 
    QPrinter pter(QPrinter::HighResolution);// Set the printer's pixels 
    // With a print rack, of course, there must be a print dialog 
    QPrintDialog *ddlg=new QPrintDialog(&pter,this);
    if(activateChildWnd()){
        ddlg->setOption(QAbstractPrintDialog::PrintSelection,true);
    }
    ddlg->setWindowTitle(" Print the document ");

    // Save the currently active child window 
    ChildWnd *childWnd=activateChildWnd();
    if(ddlg->exec()==QDialog::Accepted){
        childWnd->print(&pter);
    }

    delete ddlg;
}

void MainWindow::on_printAction_triggered()
{
    docPrint();
}

The printing function of course requires a printer , When defining the function, first define a printer specified pixel . Print, then there must be a print prompt box , Define a QPrintDialog object , And then judge whether it is an active window , If yes, set the options of the print prompt box to user selectable . Then get the current active window , Determine whether to accept printing by mode , Accept print call sub window QTextEdit Print function for print, Then delete the text print box you created .

Print preview implementation :

mainWindow.h Add function :

public:
 void docPrintPreview();// Print preview 
private slots:
 void printPreview(QPrinter*printer);
    void on_printPreviewAction_triggered();

Function implementation :

void MainWindow::docPrintPreview()
{
    QPrinter pter;
    QPrintPreviewDialog preview(&pter,this);
    connect(&preview,SIGNAL(paintRequested(QPrinter*)),
            this,SLOT(printPreview(QPrinter*)));
    preview.exec();
}


void MainWindow::printPreview(QPrinter *printer)
{
    activateChildWnd()->print(printer);
}

void MainWindow::on_printPreviewAction_triggered()
{
    docPrintPreview();
}

The main idea is to call the click slot function when clicking print preview in the main window , Click the slot function and call print preview docPrint function , First, define a printer , Because the print preview dialog box will use . In establishing signals and slots , If there is a printing demand , Just call printPreview function , To realize printing .

Program effect :

原网站

版权声明
本文为[PureヾChan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210545036951.html